转自:https://www.baeldung.com/java-system-get-property-vs-system-getenv

1. Introduction

The package java.lang is automatically imported when in a Java application. This package contains many commonly used classes from NullPointerException to Object, Math, and String.
The java.lang.System class is a final class, meaning that we cannot subclass it, therefore all methods are static.
We are going to look at the differences between two System methods for reading system properties and environment variables.
These methods are getProperty and getenv.
2. Using System.getProperty()
The Java platform uses a Properties object to provide information about the local system and configuration and we call it System Properties.
System Properties include information such as the current user, the current version of the Java runtime, and file path-name separator.
In the below code, we use System.getProperty(“log_dir”) to read the value of the property log_dir. We also make use of the default value parameter so if the property does not exist, getProperty returns of /tmp/log:
String log_dir = System.getProperty("log_dir","/tmp/log");
To update System Properties at runtime, use the method System.setProperty method:
System.setProperty("log_dir", "/tmp/log");
We can pass our own properties or configurations values to the application using the propertyName command line argument in the format:
java -jar jarName -DpropertyName=value
Setting the property of foo with a value of bar in app.jar:
java -jar app -Dfoo=”bar”
System.getProperty will always return a String.
3. Using System.getenv()
Environment Variables are key/value pairs like Properties. Many Operating Systems use Environment Variables to allow configuration information to be passed into applications.
The way to set an environment variable differs from one operating system to another. For example, in Windows, we use a System Utility application from the control panel while in Unix we use shell scripts.
When creating a process, by default it inherits a clone environment of its parent process.
The following code snippet shows using a lambda expression to print all Environment Variables.
System.getenv().forEach((k, v) -> {
    System.out.println(k + ":" + v);
});
getenv() returns a read-only Map. Trying to add values to the map throws an UnsupportedOperationException.
To obtain a single variable, call getenv with the variable name:
String log_dir = System.getenv("log_dir");
On the other hand, we can create another process from our application and add new variables to its environment.
To create a new process in Java, we use ProcessBuilder class which has a method called environment. This method returns a Map but this time the map is not read-only, meaning we can add elements to it:
ProcessBuilder pb = new ProcessBuilder(args);
Map<String, String> env = pb.environment();
env.put("log_dir", "/tmp/log");
Process process = pb.start();
4. The Differences
Although both are essentially maps that provide String values for String keys, let's look at a few differences:
We can update Properties at runtime while Environment Variables are an immutable copy of the Operating System's variables.
Properties are contained only within Java platform while Environment Variables are global at the Operating System level – available to all applications running on the same machine.
Properties must exist when packaging the application but we can create Environment Variables on the Operating System at almost any point.
5. Conclusion
Although conceptually similar, the application of both Properties and Environment Variables are quite different.
The choice between the options is often a question of scope. Using Environment Variables, the same application can be deployed to multiple machines to run different instances and can be configured at the Operating System level or even in AWS or Azure Consoles. Removing the needing to rebuild application to update config.

Java System.getProperty vs System.getenv的更多相关文章

  1. 在JAVA中 System.getProperty 和 System.setProperty 方法.

    今天着手研究TOMCAT源码. 在刚開始的时候Startup类中init方法中调用非常多次System.getProperty和System.setProperty的方法. 后来经过网上搜索才得知,这 ...

  2. JAVA System.getProperty() 与 System.getenv() 差异及示例

    System.getenv() 方法是获取指定的环境变量的值. System.getenv() 接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null. System.getP ...

  3. IDE中使用System.getProperty()获取一些属性

    使用环境:一般在项目首页或者项目后端配置中会使用到一些属性获取: package com.liuyc.study.utils; /** * 获取当前操作系统中或者当前环境中的一些默认配置 * @aut ...

  4. System.getProperty()获取系统的配置信息

    原文地址:http://www.jsjtt.com/java/Javajichu/105.html 此处记录备用. 1. 通过System.getProperty()可以获取系统的配置信息,Syste ...

  5. System.getProperty()获取系统的配置信息(系统变量)

    原文地址:http://www.jsjtt.com/java/Javajichu/105.html 此处记录备用. 1. 通过System.getProperty()可以获取系统的配置信息,Syste ...

  6. JAVA 系统变量之System.getenv()和System.getProperty() 用法

    Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与ja ...

  7. System.getProperty System.getenv 区别 log4j取法

    log4j 可以${}取系统变量相关属性  getProperty Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...

  8. 系统变量之System.getenv()和System.getProperty()

    Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与ja ...

  9. System.getenv()和System.getProperty() 的区别

    1.System.getenv() 方法是获取指定的环境变量的值.它有两种方法,一种是接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null.另外一种是不接受参数,那么返回的是 ...

随机推荐

  1. 纯CSS打造BiliBili样式博客主题

    前言 一直以来,我都在思考如何减少不必要的JS代码,仅通过CSS来实现博客园主题美化.CSS有很多魔法代码,例如:before,iconfont,order,等等,利用好这些技巧,也能实现很好美化效果 ...

  2. 为Azure DevOps Server (TFS) 配置安全访问(HTTPS with SSL)

    Contents 1. 概述 2. HTTP和HTTS比较 支持HTTP和HTTPS两种方式 要求所有连接使用HTTPS 优点: 缺点: 3. 为Azure DevOps Server 配置安全访问 ...

  3. Kubernetes Pod 资源限制

    Kubernetes Pod 资源限制 官方文档:https://kubernetes.io/docs/concepts/configuration/manage-compute-resources- ...

  4. 自动轮播swiper css实现

    @keyframes scale { 0% { transform: scale(1, 1); opacity: 0.5; z-index:; transition: opacity z-index ...

  5. JSTL+EL表达式+JSP自定义框架案例

    不会框架不要紧,我带你自定义框架 前言:这标题说的有点大了,当一回标题党,之前在学JSP的时候提到了JSTL和EL表达式,由于一直钟情于Servlet,迟迟没有更新别的,这回算是跳出来了.这回放个大招 ...

  6. Spring-AOP源码分析随手记(二)

    这次来分析下切面的执行过程. 1.怎么看? 怎么开始看源码呢?就直接从被增强的方法调用那里打断点,看看怎么执行的: 然后就来到了这: 2.初步分析 里面有段: if (this.advised.exp ...

  7. @Valid 数据校验 + 自定义全局异常信息

    关于javax.validation.Validator校验的使用 对于要校验的实体类:其需要校验的字段上需要添加注解 实际例子 使用:首先要拿到 validator的子类 Validator val ...

  8. JS实现16进制和RGB转换

    作为前端开发而言,不可避免的会遇到颜色取值,字符串和数字直接的转换,博主为此写了一个小工具,实现色值之间的在线转换. 前置知识点: parseInt, toString parseInt(value ...

  9. nodejs块级作用域

    现在让我们了解3个关键字var.let.const,的特性和使用方法. var JavaScript中,我们通常说的作用域是函数作用域,使用var声明的变量,无论是在代码的哪个地方声明的,都会提升到当 ...

  10. xcodeinstruments 内存检测

    http://blog.csdn.net/totogo2010/article/details/8233565