Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to I obtain the value of the environment variables? [duplicate]

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).

like image 898
Antrromet Avatar asked Feb 05 '11 10:02

Antrromet


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.

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.

How do you read an environment variable in UNIX Java?

In Java, the System. getenv() returns an unmodifiable string Map view of the current system environment. Map<String, String> env = System.


2 Answers

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.

like image 101
Harshit Agarwal Avatar answered Oct 20 '22 08:10

Harshit Agarwal


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());
}
like image 33
Matten Avatar answered Oct 20 '22 09:10

Matten