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";
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 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.
We use the getEnv() static method of the System class in Java to fetch the value of the specified environment variable.
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.
This code snippet should not return null in order for your problem to be resolved...
final String value = System.getenv("PE_CONF_PWD");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With