I have a strange problem setting the Linux environment from Java (1.6); specifically the "PATH" variable.
In a nutshell, I have a pipeline for running native processes, which uses java.lang.ProcessBuilder
. The user can optionally set the environment variables via a HashMap
named environment
:
ProcessBuilder pb = new ProcessBuilder(args);
Map<String, String> env = pb.environment();
if (environment != null)
env.putAll(environment);
Process process = pb.start();
The env
variable gets set properly, if I dump it to the console, with a correct value for the PATH variable. However, running the process results in a thrown Exception
:
java.io.IOException: error=2, No such file or directory
The same process runs fine with identical environment variables in the terminal shell. To test this, I ran Eclipse AFTER setting the environment in the terminal. In this case the ProcessBuilder
process runs correctly.
So what must be happening is that the ProcessBuilder
is not using the environment I set for it, but the current System environment instead.
I can't find any satisfactory answers to this problem online. Perhaps this is an OS-specific issue? Or something else I'm missing?