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 和 System.setProperty 方法.
今天着手研究TOMCAT源码. 在刚開始的时候Startup类中init方法中调用非常多次System.getProperty和System.setProperty的方法. 后来经过网上搜索才得知,这 ...
- JAVA System.getProperty() 与 System.getenv() 差异及示例
System.getenv() 方法是获取指定的环境变量的值. System.getenv() 接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null. System.getP ...
- IDE中使用System.getProperty()获取一些属性
使用环境:一般在项目首页或者项目后端配置中会使用到一些属性获取: package com.liuyc.study.utils; /** * 获取当前操作系统中或者当前环境中的一些默认配置 * @aut ...
- System.getProperty()获取系统的配置信息
原文地址:http://www.jsjtt.com/java/Javajichu/105.html 此处记录备用. 1. 通过System.getProperty()可以获取系统的配置信息,Syste ...
- System.getProperty()获取系统的配置信息(系统变量)
原文地址:http://www.jsjtt.com/java/Javajichu/105.html 此处记录备用. 1. 通过System.getProperty()可以获取系统的配置信息,Syste ...
- JAVA 系统变量之System.getenv()和System.getProperty() 用法
Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与ja ...
- System.getProperty System.getenv 区别 log4j取法
log4j 可以${}取系统变量相关属性 getProperty Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回 ...
- 系统变量之System.getenv()和System.getProperty()
Java提供了System类的静态方法getenv()和getProperty()用于返回系统相关的变量与属性,getenv方法返回的变量大多于系统相关,getProperty方法返回的变量大多与ja ...
- System.getenv()和System.getProperty() 的区别
1.System.getenv() 方法是获取指定的环境变量的值.它有两种方法,一种是接收参数为任意字符串,当存在指定环境变量时即返回环境变量的值,否则返回null.另外一种是不接受参数,那么返回的是 ...
随机推荐
- 【08月14日】A股ROE最高排名
个股滚动ROE = 最近4个季度的归母净利润 / ((期初归母净资产 + 期末归母净资产) / 2). 查看更多个股ROE最高排名 兰州民百(SH600738) - ROE_TTM:86.45% - ...
- PM学习笔记(一):解构产品经理
1.产品定义:什么是产品 来自百度百科(链接)的解释: 产品是指能够供给市场 [1] ,被人们使用和消费,并能满足人们某种需求的任何东西,包括有形的物品.无形的服务.组织.观念或它们的 ...
- 图片转PDF
目的:图片转pdf(image2pdf)依赖:fpdf.php 网址 为 http://fpdf.org/ 有文档和包 demo:step 1 : First download fpdf librar ...
- RestTemplate的三种请求方式
转载 https://blog.csdn.net/qq_36364521/article/details/84203133
- 自己搭建 NuGet.Server 环境
1. 官网 https://github.com/NuGet/NuGet.Server 下载最新的源代码 VS 发布到指定的目录,比如发布到我本地 D:\Workspace\DeploymentPro ...
- WPF TabItem设置Visibility隐藏Control内容
源自MSDN问题. 思路很简答: TabControl因为只显示TabItem的选择项的control. 所以单独的设置tabitem的control或者使用control的触发器都是不起作用的. 只 ...
- 使用VisualStudio或VisualStudio Code作为代码比较工具
最近改了了几个还是用SVN托管的老项目,用的客户端是TortoiseSVN,本身这个工具比较好用,就是那个内置的比较文件差异的Diff工具太简陋了,由于TortoiseSVN支持第三方Diff查看器的 ...
- ASP.NET Core 开发人员异常页面
UseDeveloperExceptionPage 中间件 我们谈谈在 Startup 类的 Configure()方法中以下代码: public void Configure(IApplicatio ...
- JVM指令手册
JVM指令大全 常量入栈指令 指令码 操作码(助记符) 操作数 描述(栈指操作数栈) 0x01 aconst_null null值入栈. 0x02 iconst_m1 -1(int)值入栈. 0x03 ...
- 将多个sass文件合并到一个文件中
将多个sass文件合并到一个文件中 应用场景:制作angular npm包的时候,定义的一些全局样式,自定义主题色这类的情况下,多个scss文件会要合并成一个文件并写到dist文件里,发布到仓库中. ...