How can I get System variable value in Java?

JavaEnvironment Variables

Java Problem Overview


How can I get the System Variable value which is present in

MyComputer -> Properties -> Advanced -> Environment Variables -> System Variables

in Java?

Edit

I have used System.getenv() method.

It is printing value if I give

System.out.println(System.getenv("JAVA_HOME"));

and it is showing null value if I try the same for system variable created by me

System.out.println(System.getenv("DBE"));

Java Solutions


Solution 1 - Java

Use the System.getenv(String) method, passing the name of the variable to read.

Solution 2 - Java

To clarify, system variables are the same as environment variables. User environment variables are set per user and are different whenever a different user logs in. System wide environment variables are the same no matter what user logs on.

To access either the current value of a system wide variable or a user variable in Java, see below:

String javaHome = System.getenv("JAVA_HOME");

For more information on environment variables see this wikipedia page.

Also make sure the environment variable you are trying to read is properly set before invoking Java by doing a:

echo %MYENVVAR%

You should see the value of the environment variable. If not, you may need to reopen the shell (DOS) or log off and log back on.

Solution 3 - Java

There are a few details of interest when getting system/environment properties.

First, System.getenv(String) was introduced way-back-when, then deprecated. The deprecation (foolishly, IHMO) continued http://java.sun.com/j2se/1.4.2/docs/api/java/lang/System.html#getenv(java.lang.String)">all the way into JSE 1.4.

It got http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getenv(java.lang.String)">re-introduced in JSE 5.

Those are set using the Environment Variables panel in Windows. Changes to the variables may not get picked up until your current VM is shutdown, and the CMD.exe instance is exited.

In contrast to the environment properties, Java also has Java system properties, accessible through http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#getProperties()"><code>System.getProperties()</code></a>;. These variables can be initialized when the VM is started using a series -Dname=value command line arguments. For example, the values for the properties maxInMemory and pagingDirectory are set in the command below:

C:&gt; java.exe -DmaxInMemory=100M -DpagingDirectory=c:\temp -jar myApp.jar
These properties can be modified at runtime, barring security policy restrictions.

Solution 4 - Java

Actually the variable can be set or not, so, In Java 8 or superior its nullable value should be wrapped into an Optional object, which allows really good features. In the following example we gonna try to obtain the variable ENV_VAR1, if it doesnt exist we may throw some custom Exception to alert about it:

String ENV_VAR1 = Optional.ofNullable(System.getenv("ENV_VAR1")).orElseThrow(
  () -> new CustomException("ENV_VAR1 is not set in the environment"));

Solution 5 - Java

Google says to check out getenv():

> Returns an unmodifiable string map view of the current system environment.

I'm not sure how system variables differ from environment variables, however, so if you could clarify I could help out more.

Solution 6 - Java

Have you tried rebooting since you set the environment variable?

It appears that Windows keeps it's environment variable in some sort of cache, and rebooting is one method to refresh it. I'm not sure but there may be a different method, but if you are not going to be changing your variable value too often this may be good enough.

Solution 7 - Java

As mentioned by sombody above, restarting eclipse worked for me for the user defined environment variable. After I restart eclipse IDE, System.getenv() is picking up my environment variable.

Solution 8 - Java

Are you on a linux system? If so be sure you are exporting your variable.

myVar=testvalue; export myVar

I get null unless I use export to define the value globally.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionrajaView Question on Stackoverflow
Solution 1 - JavaRobView Answer on Stackoverflow
Solution 2 - JavaElijahView Answer on Stackoverflow
Solution 3 - JavaDilum RanatungaView Answer on Stackoverflow
Solution 4 - JavaEliuXView Answer on Stackoverflow
Solution 5 - JavaChris BunchView Answer on Stackoverflow
Solution 6 - JavatrilobiteView Answer on Stackoverflow
Solution 7 - JavavoidMainReturnView Answer on Stackoverflow
Solution 8 - JavakevingView Answer on Stackoverflow