Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting environment variable value in java

An environment variable has been set in windows machine (windows 7) and trying to get the value of the variable from the following java code. However, it returns a null value.

final String value = System.getenv("PE_CONF_PWD"); System.out.println(value); //print null 

Now if I execute the following code, it throws a security exception that means that the user does not have the variable.

SecurityManager m = new SecurityManager(); m.checkPropertyAccess("PE_CONF_PWD"); // java.security.AccessControlException: access denied ("java.util.PropertyPermission" "PE_CONF_PWD" "read")  

Later I have added permission in the JRE security policy file and add the following entry but still it returns null value.

permission java.lang.RuntimePermission "getenv.PE_CONF_PWD"; 
like image 560
Shamim Ahmmed Avatar asked Aug 08 '13 13:08

Shamim Ahmmed


People also ask

How do I get values from environment variables?

To display the values of environment variables, use the printenv command. If you specify the Name parameter, the system only prints the value associated with the variable you requested.

How do you read an environment variable in Java?

How to get the value of Environment variables? The System class in Java provides a method named System. getenv() which can be used to get the value of an environment variable set in the current system.

Which method is used to get the value of and environment variable?

We use the getEnv() static method of the System class in Java to fetch the value of the specified environment variable.

What is an environment variable Java?

Many operating systems use environment variables to pass configuration information to applications. Like properties in the Java platform, environment variables are key/value pairs, where both the key and the value are strings.


1 Answers

This code snippet should not return null in order for your problem to be resolved...

final String value = System.getenv("PE_CONF_PWD"); 
like image 147
Ankit Avatar answered Sep 17 '22 22:09

Ankit