Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.getenv() method - deprecated or not?

Many places suggest that the System.getenv() JDK method has been deprecated. Many places suggest that it has been reinstated.

Is there a better API that provides the functionality to get environment settings in subsequent JDKs?

Or is System.getenv() still the preferred way to get environment variables / properties?

like image 591
Chet Avatar asked Jul 02 '15 06:07

Chet


2 Answers

System.getenv() and System.getenv(String) were introduced in the very early days of java, and then deprecated in 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.

like image 149
Mureinik Avatar answered Sep 29 '22 08:09

Mureinik


In the Java 8 API docs the method is not deprecated. It has references to getenv(String) but no other alternatives. 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. 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). Given that that API was introduced in Java 1.5 it is also highly backwards compatible.

like image 23
simbo1905 Avatar answered Sep 29 '22 08:09

simbo1905