Reputation: 21
I came through of previous questions published in stackoverflow. Thus, my Java application modifies the System environment, such as:
Map<String, String> env = System.getenv();
...
...
map.put("SOME_VAR_ENV", "SOME_KEY_VAR");*
This seem works fine, since some sentences later:
System.out.println(System.getenv("SOME_VAR_ENV"));*
prints SOME_KEY_VAR
.
And, here is my problem. At the next sentence, my Java application yields the control to other Java app. My Java app. invokes to an external Java Class that, as far as I know, it must create a new child process to run on. However, it new process has not preserved the new environment variable (SOME_VAR_ENV).
I have no idea of what's hapening. Why the second application has not preserved the environment? Any idea or help is welcome :)
Thanks!
Upvotes: 2
Views: 426
Reputation: 51445
Look into writing, and then reading, a Java Properties file,
Upvotes: 0
Reputation: 298818
Use the ProcessBuilder
API to start the child process, it lets you set environment variables:
ProcessBuilder pb = new ProcessBuilder("myCommand", "myArg1", "myArg2");
Map<String, String> env = pb.environment();
env.put("VAR1", "myValue");
env.remove("OTHERVAR");
env.put("VAR2", env.get("VAR1") + "suffix");
pb.directory(new File("myDir"));
Process p = pb.start();
There is no cross-platform way in Java to set environment variables of the calling context.
Upvotes: 2