Java System.getProperty vs System.getenv – Java System.getProperty vs System.getenv

最后修改: 2018年 6月 21日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

The package java.lang is automatically imported when in a Java application. This package contains many commonly used classes, from NullPointerException to Object, Math, and String.

在Java应用程序中,java.lang包被自动导入。这个包包含许多常用的类,从NullPointerExceptionObjectMathString

The java.lang.System class is a final class, meaning that we can’t subclass it, and therefore, all methods are static.

java.lang.System类是一个final类,意味着我们不能对其进行子类化,因此,所有方法都是static

In this tutorial, we’ll explore the differences between two System methods for reading system properties and environment variables. These methods are getProperty and getenv.

在本教程中,我们将探讨用于读取系统属性和环境变量的两个System方法之间的差异。这些方法是getPropertygetenv

2. Using System.getProperty()

2.使用System.getProperty()

The Java platform uses a Properties object to provide information about the local system and configuration, and we call it System Properties.

Java平台使用一个Properties对象来提供关于本地系统和配置的信息,我们称之为系统属性

System Properties include information such as the current user, the current version of the Java runtime, and the file path-name separator.

系统属性包括诸如当前用户、Java运行时的当前版本和文件路径名分隔符等信息。

In the below code, we use System.getProperty(“log_dir”) to read the value of the property log_dir. We also make use of the default value parameter, so if the property doesn’t exist, getProperty returns /tmp/log:

在下面的代码中,我们使用System.getProperty(“log_dir”) 来读取属性log_dir的值。我们还利用了默认值参数,所以如果该属性不存在,getProperty将返回/tmp/log

String log_dir = System.getProperty("log_dir","/tmp/log");

To update System Properties at runtime, we use the System.setProperty method:

为了在运行时更新系统属性,我们使用System.setProperty方法。

System.setProperty("log_dir", "/tmp/log");

We can pass our own properties or configurations values to the application using the propertyName command line argument:

我们可以使用propertyName命令行参数向应用程序传递我们自己的属性或配置值。

java -jar jarName -DpropertyName=value

We set the property of foo with a value of bar in app.jar:

我们在app.jar中设置了foo的属性,其值为bar。

java -jar app -Dfoo="bar"

System.getProperty will always return a String.

System.getProperty将总是返回一个String.

3. Using System.getenv()

3.使用System.getenv()

Environment Variables are key/value pairs like Properties. Many Operating Systems use Environment Variables to allow configuration information to be passed into applications.

环境变量是类似于属性的键/值对。许多操作系统使用环境变量来允许配置信息被传递到应用程序中。

The way to set an environment variable differs from one operating system to another. For example, in Windows, we use a System Utility application from the control panel, while in Unix, we use shell scripts.

设置环境变量的方式在不同的操作系统中是不同的。例如,在Windows中,我们使用控制面板中的系统实用程序,而在Unix中,我们使用shell脚本。

When creating a process, it inherits a clone environment of its parent process by default.

当创建一个进程时,它默认会继承其父进程的克隆环境。

The following code snippet illustrates using a lambda expression to print all Environment Variables:

下面的代码片段说明了使用lambda表达式来打印所有环境变量。

System.getenv().forEach((k, v) -> {
    System.out.println(k + ":" + v);
});

getenv() returns a read-only Map. Trying to add values to the map throws an UnsupportedOperationException.

getenv()返回一个只读的Map.试图向map添加值会抛出一个UnsupportedOperationException

To obtain a single variable, we can call getenv with the variable name:

为了获得一个单一的变量,我们可以用变量名调用getenv

String log_dir = System.getenv("log_dir");

On the other hand, we can create another process from our application and add new variables to its environment.

另一方面,我们可以从我们的应用程序中创建另一个进程,并在其环境中添加新的变量。

To create a new process in Java, we can use the ProcessBuilder class, which has a method called environment. This method returns a Map, but this time the map is not read-only, meaning we can add elements to it:

要在Java中创建一个新的进程,我们可以使用ProcessBuilder类,它有一个名为environment的方法。这个方法返回一个Map,,但这次的map不是只读的,这意味着我们可以向它添加元素。

ProcessBuilder pb = new ProcessBuilder(args);
Map<String, String> env = pb.environment();
env.put("log_dir", "/tmp/log");
Process process = pb.start();

4. The Differences

4.差异

Although both are essentially maps that provide String values for String keys, let’s look at a few differences:

虽然两者本质上都是为String键提供String值的映射,但让我们看看一些区别。

  1. We can update Properties at runtime, while Environment Variables are an immutable copy of the Operating System’s variables.
  2. Properties are contained only within the Java platform, while Environment Variables are global at the Operating System level, available to all applications running on the same machine.
  3. Properties must exist when packaging the application, but we can create Environment Variables on the Operating System at almost any point.

5. Conclusion

5.总结

Although conceptually similar, the application of Properties and Environment Variables are quite different.

虽然概念上相似,但属性和环境变量的应用却有很大不同。

The choice between the two is often a question of scope. Using Environment Variables, the same application can be deployed to multiple machines to run different instances, and can be configured at the Operating System level, or even in AWS or Azure Consoles. This removes the need to rebuild the application to update the config.

两者之间的选择往往是一个范围的问题。使用环境变量,同一个应用程序可以被部署到多台机器上运行不同的实例,并且可以在操作系统层面上进行配置,甚至可以在AWS或Azure控制台进行配置。这消除了重建应用程序以更新配置的需要。

Always remember that getProperty follows camel-case convention, and getenv doesn’t.

请永远记住,getProperty遵循骆驼字母大小写惯例,而getenv则不然。