The Java Tutorials have been written for JDK 8.Java教程是为JDK 8编写的。Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available.本页中描述的示例和实践没有利用后续版本中引入的改进,并且可能使用不再可用的技术。See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases.有关Java SE 9及其后续版本中更新的语言特性的摘要,请参阅Java语言更改。
See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.有关所有JDK版本的新功能、增强功能以及已删除或不推荐的选项的信息,请参阅JDK发行说明。
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.要了解如何将环境变量传递给系统上的应用程序,请参阅系统文档。
On the Java platform, an application uses 在Java平台上,应用程序使用System.getenv
to retrieve environment variable values. 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
返回null
。The 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); } } } }
When a Java application uses a 当Java应用程序使用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. ProcessBuilder
对象创建新进程时,传递给新进程的默认环境变量集与提供给应用程序虚拟机进程的环境变量集相同。The application can change this set using 应用程序可以使用ProcessBuilder.environment
.ProcessBuilder.environment
更改此集合。
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 例如,Windows在名为USERNAME
, while UNIX implementations might provide the user name in USER
, LOGNAME
, or both.USERNAME
的环境变量中提供用户名,而UNIX实现可能在USER
、LOGNAME
或两者中提供用户名。
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
中可用。