System.getenv()与System.getProperties()的区别

System.getenv()

public static String getenv(String name)

Gets the value of the specified environment variable. An environment variable is a system-dependent external named value. If a security manager exists, its checkPermission method is called with a RuntimePermission("getenv."+name) permission. This may result in a SecurityException being thrown. If no exception is thrown the value of the variable name is returned.

System properties and environment variables are both conceptually mappings between names and values. Both mechanisms can be used to pass user-defined information to a Java process. Environment variables have a more global effect, because they are visible to all descendants of the process which defines them, not just the immediate Java subprocess. They can have subtly different semantics, such as case insensitivity, on different operating systems. For these reasons, environment variables are more likely to have unintended side effects. It is best to use system properties where possible. Environment variables should be used when a global effect is desired, or when an external system interface requires an environment variable (such as PATH).

On UNIX systems the alphabetic case of name is typically significant, while on Microsoft Windows systems it is typically not. For example, the expression System.getenv("FOO").equals(System.getenv("foo")) is likely to be true on Microsoft Windows.

Parameters:

name - the name of the environment variable

Returns:

the string value of the variable, or null if the variable is not defined in the system environment

Throws:

NullPointerException - if name is null SecurityException - if a security manager exists and its checkPermission method doesn't allow access to the environment variable name See Also: getenv(), ProcessBuilder.environment()

System.getProperties()

public static Properties getProperties()

Determines the current system properties. First, if there is a security manager, its checkPropertiesAccess method is called with no arguments. This may result in a security exception.

The current set of system properties for use by the getProperty(String) method is returned as a Properties object. If there is no current set of system properties, a set of system properties is first created and initialized. This set of system properties always includes values for the following keys:

Multiple paths in a system property value are separated by the path separator character of the platform.

Note that even if the security manager does not permit the getProperties operation, it may choose to permit the getProperty(String) operation.

Returns:

the system properties

Throws:

SecurityException - if a security manager exists and its checkPropertiesAccess method doesn't allow access to the system properties.

See Also:

setProperties(java.util.Properties), SecurityException,SecurityManager.checkPropertiesAccess(), Properties

区别

getenv()

获取的是操作系统的环境变量,比如PATH,JAVA_HOME,大部分通过/etc/profile,~/.bash_profile,~/.bashrc,或者使用export独立设置的系统变量。

getProperties()

获取的是jvm虚拟机的变量,大部分通过-D参数指定,或者一些自带变量。如下图所示。
输入图片说明

转载于:https://my.oschina.net/yulongblog/blog/738394

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值