Documentation

The Java™ Tutorials
Hide TOC
Environment Variables环境变量
Trail: Essential Java Classes
Lesson: The Platform Environment
Section: Configuration Utilities

Environment Variables环境变量

Many operating systems use environment variables to pass configuration information to applications. 许多操作系统使用环境变量将配置信息传递给应用程序。Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings. 与Java平台中的属性一样,环境变量是键/值对,其中键和值都是字符串。The conventions for setting and using environment variables vary between operating systems, and also between command line interpreters. 设置和使用环境变量的约定因操作系统而异,也因命令行解释器而异。To learn how to pass environment variables to applications on your system, refer to your system documentation.要了解如何将环境变量传递给系统上的应用程序,请参阅系统文档。

Querying Environment Variables查询环境变量

On the Java platform, an application uses System.getenv to retrieve environment variable values. 在Java平台上,应用程序使用System.getenv检索环境变量值。Without an argument, getenv returns a read-only instance of java.util.Map, where the map keys are the environment variable names, and the map values are the environment variable values. 没有参数,getenv返回java.util.Map的只读实例,其中映射键是环境变量名,映射值是环境变量值。This is demonstrated in the EnvMap example:这在EnvMap示例中进行了演示:

import java.util.Map;

public class EnvMap {
    public static void main (String[] args) {
        Map<String, String> env = System.getenv();
        for (String envName : env.keySet()) {
            System.out.format("%s=%s%n",
                              envName,
                              env.get(envName));
        }
    }
}

With a String argument, getenv returns the value of the specified variable. 使用String参数,getenv返回指定变量的值。If the variable is not defined, getenv returns null. 如果未定义变量,则getenv返回nullThe Env example uses getenv this way to query specific environment variables, specified on the command line:Env示例以这种方式使用getenv来查询命令行上指定的特定环境变量:

public class Env {
    public static void main (String[] args) {
        for (String env: args) {
            String value = System.getenv(env);
            if (value != null) {
                System.out.format("%s=%s%n",
                                  env, value);
            } else {
                System.out.format("%s is"
                    + " not assigned.%n", env);
            }
        }
    }
}

Passing Environment Variables to New Processes将环境变量传递给新进程

When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. 当Java应用程序使用ProcessBuilder对象创建新进程时,传递给新进程的默认环境变量集与提供给应用程序虚拟机进程的环境变量集相同。The application can change this set using ProcessBuilder.environment.应用程序可以使用ProcessBuilder.environment更改此集合。

Platform Dependency Issues平台依赖性问题

There are many subtle differences between the way environment variables are implemented on different systems. 在不同的系统上实现环境变量的方式有许多细微的差别。For example, Windows ignores case in environment variable names, while UNIX does not. 例如,Windows会忽略环境变量名称中的大小写,而UNIX则不会。The way environment variables are used also varies. 环境变量的使用方式也各不相同。For example, Windows provides the user name in an environment variable called USERNAME, while UNIX implementations might provide the user name in USER, LOGNAME, or both.例如,Windows在名为USERNAME的环境变量中提供用户名,而UNIX实现可能在USERLOGNAME或两者中提供用户名。

To maximize portability, never refer to an environment variable when the same value is available in a system property. 为了最大限度地提高可移植性,当系统属性中存在相同的值时,切勿引用环境变量。For example, if the operating system provides a user name, it will always be available in the system property user.name.例如,如果操作系统提供用户名,则它将始终在系统属性user.name中可用。


Previous page: Command-Line Arguments
Next page: Other Configuration Utilities