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.另外一种是不接受参数,那么返回的是 ...
随机推荐
- ajax.readyState和HTTP状态码的提示
ajax.readyState 0 -(未初始化)还没有调用send()方法 1 -(载入)已调用send()方法,正在发送请求 2 -(载入完成)send()方法执行完成,已经接收到全部响应内容 3 ...
- 谈谈vue.js中methods watch和compute的区别和联系
methods,watch和computed都是以函数为基础的,但各自却都不同: 1.watch和computed都是以Vue的依赖追踪机制为基础的,它们都试图处理这样一件事情:当某一个数据(称它为依 ...
- 【VSFTP服务】vsftpd文件传输协议
vsftpd文件传输协议 系统环境:CentOS Linux release 7.6.1810 (Core) 一.简介 FTP(文件传输协议)全称是:Very Secure FTP Server. ...
- redis之HyperLogLog
HyperLogLog 提供不精确的去重计数方案,虽然不精确但是也不是非常不精确,标准误差是 0.81%. 使用方法 HyperLogLog 提供了两个指令 pfadd 和 pfcount,根据字面意 ...
- 使用IDEA的Git插件上传项目教程
如何使用IDEA的Git插件上传项目 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载 二.注册码云账号 搜索gitee码云插件并安装
- 如何使用gitlab自建golang基础库
这里以go mod方式建立golang基础库 一.gitlab创建项目golib 地址为gitlab.xxx.com/base/golib 示例如下 go mod初始化命令 go mod init g ...
- 【微信原生支付】服务商模式-小微商户专属接口:小微商户新增对应APPID关联API
文档地址:https://pay.weixin.qq.com/wiki/doc/api/xiaowei.php?chapter=20_3&index=3 这个接口比较特殊不需要nonce_st ...
- Asp.netCore 3.0 Web 实现Oauth2.0微信授权登陆的测试
1:Oauth2.0授权的流程截图 官方流程如下: 1 第一步:用户同意授权,获取code 2 第二步:通过code换取网页授权access_token 3 第三步:刷新access_token(如果 ...
- IIS创建文件服务器(WebDAV)
1.安装IIS,选择安装WEBDAV组件.然后新建站点,站点目录不需要额外设置任何权限 安装完成后组件: 2.配置WebDAV: 添加创作规则:允许某用户写入,其他所有用户读取.(写入规则必须要放在第 ...
- Android studio R文件丢失或错误解决方法
android studio中有时引用资源会出现R文件丢失或报错,大多数情况下是由于引入资源时R文件没有及时更新造成的 (在代码没有错误或资源引用没有错误的前提下) 注意:资源文件的文件名必须小写,即 ...