简体   繁体   English

System.getenv() 方法 - 是否已弃用?

[英]System.getenv() method - deprecated or not?

Many places suggest that the System.getenv() JDK method has been deprecated.许多地方暗示System.getenv() JDK 方法已被弃用。 Many places suggest that it has been reinstated.很多地方都暗示它已经恢复了。

Is there a better API that provides the functionality to get environment settings in subsequent JDKs?是否有更好的 API 提供在后续 JDK 中获取环境设置的功能?

Or is System.getenv() still the preferred way to get environment variables / properties?或者System.getenv()仍然是获取环境变量/属性的首选方法?

System.getenv() and System.getenv(String) were introduced in the very early days of java, and then deprecated in Java 1.2(!). System.getenv()System.getenv(String)在 Java 的早期被引入,然后在 Java 1.2(!) 中被弃用。

Java 5 reinstated them (see bug 4199068 ), and since there's been no sign of these methods going anywhere or even being deprecated again for the last dozen years or so, I think it's safe to assume that they're here to stay, and use them as you need them. Java 5 恢复了它们(请参阅错误 4199068 ),并且由于在过去的十几年左右的时间里没有任何迹象表明这些方法会随处可见甚至被再次弃用,我认为可以安全地假设它们会继续存在并使用根据您的需要使用它们。

In theJava 8 API docs the method is not deprecated.Java 8 API 文档中,该方法并未被弃用。 It has references to getenv(String) but no other alternatives.它有对getenv(String)引用,但没有其他替代方法。 Since getenv() returns a Map<String,String> from which you could just get(String) you may as well just call getenv(String) to get the value you are looking for else null if it is not defined.由于getenv()返回一个Map<String,String> ,您可以get(String)您也可以调用getenv(String)来获取您正在寻找的值,否则如果未定义则为null As those API docs are authoritative you can conclude that getEnv() is not deprecated in the current version Java nor is there a better recommended alternative other than getenv(String) .由于这些 API 文档是权威的,您可以得出结论, getEnv()在当前版本的 Java 中没有被弃用,也没有比getenv(String)更好的推荐替代方案。 Given that that API was introduced in Java 1.5 it is also highly backwards compatible.鉴于该 API 是在 Java 1.5 中引入的,它还具有高度的向后兼容性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM