转自: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. FineUIPro v6.0.1 小版本更新!

    这次修正了 v6.0.0版本的几个问题,建议所有用户升级到此版本: +修正调用F.addMainTab时可能出现JS错误的问题(34484135,1450561644).    -仅在未调用F.ini ...

  2. redis.windows.conf 配置注释

    . daemonize no Redis默认不是以守护进程的方式运行,可以通过该配置项修改,使用yes启用守护进程 . pidfile /var/run/redis_6379.pid 当Redis以守 ...

  3. Shell基本运算符之布尔运算符、逻辑运算符

    Shell基本运算符 =============================摘自与菜鸟教程=============================== 1.布尔运算符 ! 非运算,表达式为tru ...

  4. vue前端post请求之坑

    最近用的vue请求数据,坑死,还是对前端vue框架不熟. 与后端通信有问题,要么是json加入到url有问题.要么是json解析有问题. 解决方法: 1.请求参数一个用url传 var json=[{ ...

  5. 最锋利的Visual Studio Web开发工具扩展:Web Essentials详解【转】

    Web Essentials是目前为止见过的最好用的VS扩展工具了,具体功能请待我一一道来. 首先,从Extension Manager里安装:最新版本是19号发布的2.5版 然后重启你的VS开发环境 ...

  6. Codeforces 1256A 1257A

    题目链接:https://codeforces.com/problemset/problem/1256/A A. Payment Without Change time limit per test ...

  7. 进程调度算法spf,fpf,时间片轮转算法实现

    调度的基本概念:从就绪队列中按照一定的算法选择一个进程并将处理机分配给它运行,以实现进程并发地执行. 进程信息 struct node { string name;//进程名称 int id;//进程 ...

  8. JAVA性能监控与调优参考文档链接

    JAVA性能监控与调优参考文档链接 jdk8工具集 https://docs.oracle.com/javase/8/docs/technotes/tools/unix/index.htmlTroub ...

  9. 读oc52个有效方法的总结

    这本书主要是对于oc语言的代码优化和一些我们不知道的精华.全书分为7章节 1.熟悉oc语言 第一条:了解oc的语言起源 主要是对于oc语言的起源介绍和oc语言的特点进行概括,oc语言主要是使用消息结构 ...

  10. select子句

    1.order by order by 字段1 升序或者降序,字段2 升序或者降序(dsc) 默认 升序(asc) 注意:如果是分组,则应该使用对分组字段进行排序的groupby语法 group by ...