System.getenv()與System.getProperties()的區別

System.getenv()

public static String getenv(String name)java

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.express

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).bash

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.app

Parameters:jvm

name - the name of the environment variableide

Returns:ui

the string value of the variable, or null if the variable is not defined in the system environment操作系統

Throws:code

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()orm

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參數指定,或者一些自帶變量。以下圖所示。
輸入圖片說明

相關文章
相關標籤/搜索