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

原创
2016/08/26 17:07
阅读数 188

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参数指定,或者一些自带变量。如下图所示。
输入图片说明

展开阅读全文
打赏
0
3 收藏
分享
加载中
更多评论
打赏
0 评论
3 收藏
0
分享
返回顶部
顶部