根据JDK 7中的描述

Returns an unmodifiable string map view of the current system environment.


public static Map<String,String> getenv()

Returns an unmodifiable string map view of the current system environment. The environment is a system-dependent mapping from names to values which is passed from parent to child processes.

If the system does not support environment variables, an empty map is returned.

The returned map will never contain null keys or values. Attempting to query the presence of a null key or value will throw a NullPointerException. Attempting to query the presence of a key or value which is not of type String will throw a ClassCastException.

The returned map and its collection views may not obey the general contract of the Object.equals(java.lang.Object) and Object.hashCode() methods.

The returned map is typically case-sensitive on all platforms.

If a security manager exists, its checkPermission method is called with a RuntimePermission("getenv.*") permission. This may result in a SecurityException being thrown.

When passing information to a Java subprocess, system properties are generally preferred over environment variables.

    the environment as a map of variable names to values
    SecurityException - if a security manager exists and its checkPermission method doesn't allow access to the process environment
See Also:
    getenv(String), ProcessBuilder.environment()


Determines the current system properties.


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:
Key 	Description of Associated Value
java.version 	Java Runtime Environment version
java.vendor 	Java Runtime Environment vendor
java.vendor.url 	Java vendor URL
java.home 	Java installation directory
java.vm.specification.version 	Java Virtual Machine specification version
java.vm.specification.vendor 	Java Virtual Machine specification vendor 	Java Virtual Machine specification name
java.vm.version 	Java Virtual Machine implementation version
java.vm.vendor 	Java Virtual Machine implementation vendor 	Java Virtual Machine implementation name
java.specification.version 	Java Runtime Environment specification version
java.specification.vendor 	Java Runtime Environment specification vendor 	Java Runtime Environment specification name
java.class.version 	Java class format version number
java.class.path 	Java class path
java.library.path 	List of paths to search when loading libraries 	Default temp file path
java.compiler 	Name of JIT compiler to use
java.ext.dirs 	Path of extension directory or directories 	Operating system name
os.arch 	Operating system architecture
os.version 	Operating system version
file.separator 	File separator ("/" on UNIX)
path.separator 	Path separator (":" on UNIX)
line.separator 	Line separator ("\n" on UNIX) 	User's account name
user.home 	User's home directory
user.dir 	User's current working directory

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.

    the system properties
    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


网上很多使用的是getProperties。说获得系统变量,但是其实不正确。getProperties中所谓的"system properties"其实是指"java system",而非"operation system",概念完全不同,使用getProperties获得的其实是虚拟机的变量形如: -Djavaxxxx。



还有就是在JDK 1.4中该方法其实是Deprecated的,1.3也是。我认为Sun的当初设计思路是想让其屏蔽,或者可控Java对系统变量的访问能力。以下为JavaDoc 1.3, 1.4中对getenv方法的描述



public static String getenv(String name)

    Deprecated. The preferred way to extract system-dependent information is the system properties of the java.lang.System.getProperty methods and the corresponding getTypeName methods of the Boolean, Integer, and Long primitive types. For example:

             String classPath = System.getProperty("java.class.path",".");

             if (Boolean.getBoolean("myapp.exper.mode"))

    Gets an environment variable. An environment variable is a system-dependent external variable that has a string value.

        name - of the environment variable
        the value of the variable, or null if the variable is not defined.
    See Also:
        Boolean.getBoolean(java.lang.String), Integer.getInteger(java.lang.String), Integer.getInteger(java.lang.String, int), Integer.getInteger(java.lang.String, java.lang.Integer), Long.getLong(java.lang.String), Long.getLong(java.lang.String, long), Long.getLong(java.lang.String, java.lang.Long), getProperties(), getProperty(java.lang.String), getProperty(java.lang.String, java.lang.String)


意思就是说:希望使用Java的系统变量替代操作系统的变量获取,如果你想访问某个系统的环境变量(operation system properti