[英]Java system properties and environment variables
What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM? JVM 中的系统属性System.getProperties()和环境变量System.getenv()有什么区别?
System properties are set on the Java command line using the -Dpropertyname=value
syntax.使用-Dpropertyname=value
语法在 Java 命令行上设置系统属性。 They can also be added at runtime using System.setProperty(String key, String value)
or via the various System.getProperties().load()
methods.它们也可以在运行时使用System.setProperty(String key, String value)
或通过各种System.getProperties().load()
方法添加。
To get a specific system property you can use System.getProperty(String key)
or System.getProperty(String key, String def)
.要获取特定的系统属性,您可以使用System.getProperty(String key)
或System.getProperty(String key, String def)
。
Environment variables are set in the OS, eg in Linux export HOME=/Users/myusername
or on Windows SET WINDIR=C:\Windows
etc, and, unlike properties, may not be set at runtime.环境变量在操作系统中设置,例如在 Linux export HOME=/Users/myusername
或 Windows SET WINDIR=C:\Windows
等中,并且与属性不同,可能不会在运行时设置。
To get a specific environment variable you can use System.getenv(String name)
.要获取特定的环境变量,您可以使用System.getenv(String name)
。
I think the difference between the two boils down to access.我认为两者之间的区别归结为访问。 Environment variables are accessible by any process and Java system properties are only accessible by the process they are added to.任何进程都可以访问环境变量,并且 Java 系统属性只能由添加它们的进程访问。
Also as Bohemian stated, env variables are set in the OS (however they 'can' be set through Java) and system properties are passed as command line options or set via setProperty()
.同样正如 Bohemian 所说,环境变量是在操作系统中设置的(但是它们“可以”通过 Java 设置),系统属性作为命令行选项传递或通过setProperty()
设置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.