Possible Duplicate:
Access shell environment variables Java
I've created a stand-alone java application in linux.
How can I obtain the value of the environment variables (e.g. assigned in the .bashrc
file).
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.
We use the getEnv() static method of the System class in Java to fetch the value of the specified environment variable.
In Java, the System. getenv() returns an unmodifiable string Map view of the current system environment. Map<String, String> env = System.
For obtaining only one System Variable use the following code:
String sysEnvStr = System.getenv("JAVA_HOME");
If it returns null
then make changes in your .bashrc
file. Try exporting that particular variable.
See this howto:
// just one
System.out.println("PATH = " + System.getenv("PATH"));
// all of them
Map env = System.getenv();
for (Iterator it=env.entrySet().iterator(); it.hasNext(); ) {
Map.Entry entry = (Map.Entry)it.next();
System.out.println(entry.getKey() + " = " + entry.getValue());
}
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