views:

62

answers:

2

Does anyone know how to access the environment variables of the standard shell environment in Java? I am using the ProcessBuilder class and I have to specify specific environment variables used in a shell script I am running, these variables exist in the standard shell environment.

Accessing ProcessBuilder environment does not work.

A: 
processBuilder.environment().get("variablename");
dogbane
No that does not work, I have printed all the variables in the Process Builder's environment and the ones I am looking for are absent. However when type env in the terminal they are present, can you account for this?
denis
did you launch the java app from the same terminal?
dogbane
+2  A: 

You can get at the environment variables that existed when your program was created through System.getenv():

http://download.oracle.com/javase/tutorial/essential/environment/env.html

When a Java application uses a ProcessBuilder object to create a new process, the default set of environment variables passed to the new process is the same set provided to the application's virtual machine process. The application can change this set using ProcessBuilder.environment.

It looks like your child process should get your environment automatically.

dsolimano
I have tired this type of approach - see above comment.
denis
@denis, how is your process started?
dsolimano
What Java version are you using?
Eugene Kuleshov