何时使用环境变量与系统属性? [英] When to use environment variables vs. system properties?

查看:124
本文介绍了何时使用环境变量与系统属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我们可以将这些设置为 APP_HOME = / path / to / file 导出 .profile 或沿着这些行的东西),并将其作为 System.getenv(APP_HOME)



或者使用属性作为 -DAPP_HOME = / path / to / file 并将其作为 System.getProperty(APP_HOME)访问



现在..任何一个将使该值可用于应用程序的立场,但是首选方法?为什么?何时?

解决方案

如果您使用Java 1.3或1.4(和1.2,IIRC),则应使用系统属性,因为 System.getenv 已被弃用。它在Java 1.5中恢复。有关的错误报告可以在 here 找到。



您可以同时使用两者。搜索键的系统属性,如果不存在,搜索环境。这给了你最好的两个世界。



这些确实不一样:一个需要明确设置值,另一个不是。另外,请注意,环境是放置一些字符串以实现互操作性的一个方便的地方。


I wonder which of the following is a preferred approach?

We can set things up as APP_HOME=/path/to/file (export in .profile or something along those lines) and access it as System.getenv("APP_HOME")

Or, alternatively using properties as -DAPP_HOME=/path/to/file and access it as System.getProperty("APP_HOME")

Now .. either one will make the value available for the application stand point, but is either approach preferred? Why? When?

解决方案

If you are using Java 1.3 or 1.4 (and 1.2, IIRC), you should be using system properties, since System.getenv was deprecated. It was reinstated in Java 1.5. The relevant bug report can be found here.

You can use both. Search system properties for the key, and if it's not there, search the environment. This gives you the best of both worlds.

These really aren't the same thing: One requires the value to be set explicitly, and the other not. Also, note that the environment is a convenient place to put some strings for interoperability.

这篇关于何时使用环境变量与系统属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆