Java系统属性和环境变量
系统属性之间有什么区别 系统.getProperties() 和环境变量 System.getenv() 在 JVM 中?
What's the difference between system properties System.getProperties() and environment variables System.getenv() in a JVM?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
系统属性是使用
-Dpropertyname=value
语法在Java命令行上设置的。它们也可以在运行时添加使用
System.setProperty(String key, String value)
或通过各种系统.getProperties().load()
方法。要获取特定的系统属性,您可以使用
System.getProperty(String key)
或System.getProperty(String key, String def)
.环境变量在操作系统中设置,例如在Linux中
export HOME=/Users/myusername
或在Windows中SET WINDIR =C:\Windows
等,并且,与属性不同,可能不在运行时设置。
要获取特定的环境变量,您可以使用
System.getenv(String name)
.System properties are set on the Java command line using the
-Dpropertyname=value
syntax. They can also be added at runtimeusing
System.setProperty(String key, String value)
or via the variousSystem.getProperties().load()
methods.To get a specific system property you can use
System.getProperty(String key)
orSystem.getProperty(String key, String def)
.Environment variables are set in the OS, e.g. in Linux
export HOME=/Users/myusername
or on WindowsSET WINDIR=C:\Windows
etc,and, unlike properties, may not be set at runtime.
To get a specific environment variable you can use
System.getenv(String name)
.我认为两者之间的区别归结为访问权限。任何进程都可以访问环境变量,而 Java 系统属性只能由添加它们的进程访问。
另外,正如 Bohemian 所说,环境变量是在操作系统中设置的(但是它们“可以”通过 Java 设置),系统属性作为命令行选项传递或通过
setProperty()
。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.
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()
.