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.另外一种是不接受参数,那么返回的是 ...
随机推荐
- Paper | Quality assessment of deblocked images
目录 1. 故事 2. 失真变化 3. 方法(PSNR-B) 4. 实验 这篇文章提出了一个PSNR-B指标,旨在衡量 压缩图像的块效应强度 或 去块效应后的残留块效应强度(比较去块效应算法的优劣). ...
- 数据仓库009 - SQL命令实战 - where GROUP BY join 部门综合案例
一.where条件 WHERE 子句中主要的运算符,可以在 WHERE 子句中使用,如下表: 运算符 描述 = 等于 <> 不等于.注释:在 SQL 的一些版本中,该操作符可被写成 != ...
- torch_11_风格迁移和cycleGAN
1,A Neural Algorithm of atistic Style https://axiv.org/pdf/1508.06576.pdf 如何定义图片的内容,风格: 定义内容:在vggnet ...
- 【Oracle】 RMAN命令汇总
RMAN命令汇总 2013年写了关于RMAN命令的汇总,先转换为MD文档,温故而知新. 1.进入RMAN 进入本地数据库 [oracle@oracle-n1 ~]$ rman target / 进入远 ...
- 一个简单的 WPF 程序,用于显示实时时间
直接贴代码了: TimeShowerWindow.xaml <Window x:Class="HelloWorld.TimeShowerWindow" xmlns=" ...
- WebApi接口安全性 接口权限调用、参数防篡改防止恶意调用
背景介绍 最近使用WebApi开发一套对外接口,主要是数据的外送以及结果回传,接口没什么难度,采用WebApi+EF的架构简单创建一个模板工程,使用template生成一套WebApi接口,去掉put ...
- SQLAlchemy--基本增删改查
目录 简介 安装 组成部分 简单使用 执行原生sql(不常用) orm使用(重点) 常用数据类型 Column常用参数 常用操作(CURD) 创建映射类的实例 创建会话Session 增加add()/ ...
- Asp.Net MVC强类型页面获取值几种方式
方式一 (V:视图) @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="v ...
- 4 CVE-2012-0158 漏洞分析
操作系统:Windows7 32位 专业版 Office:2003sp3_20120218.exe 工具:OD和IDA 1.漏洞的本质:程序编写时未对内存拷贝函数的长度参数进行足够严谨的验证,造成的堆 ...
- spring boot的异常处理
原文:https://blog.csdn.net/tianyaleixiaowu/article/details/70145251 全局异常处理是个比较重要的功能,一般在项目里都会用到. 我大概把一次 ...