Question

I am having troubles with Java's ProcessBuilder on an Eclipse plugin I'm developing. I correctly set the environment before calling the start() method, but when I run the program, it always returns a command not found error.

When I call the command via command line it works perfectly.

When I start the eclipse with the environment as I require, the command is found and the program works fine.

Only when I set the environment programatically, the program fails.

Here is what I have:

ProcessBuilder pb = new ProcessBuilder("my_command", file, output);
Map<String, String> env = pb.environment();
env.put("PATH", env.get("PATH") + File.pathSeparator + env1 + File.pathSeparator + env2);
Process p = pb.start();
...

where, env1 and env2 are the paths I want to add to the PATH variable...

What is wrong in this code?

Thank you in advance!

Was it helpful?

Solution

I think, the environment you set on the ProcessBuilder is only what is passed to the new process but not what is used by the builder itself. Try setting the environment variables of your Java process before trying to start the new process.

Edit:

Seeing that it may not be possible to alter the Java process's environment, I believe you have to come up with some work-around.

When you already know the path(s) you are looking for you can of course figure out the full path to "my_command" yourself, about so:

String commandString;

if ( new File(env1 + "/my_command").isFile() ) {
  commandString = env1 + "/my_command";
} else
if ( new File(env2 + "/my_command").isFile() ) {
  commandString = env2 + "/my_command";
}

ProcessBuilder pb = new ProcessBuilder(commandString, file, output);

Might be impractical though, if "my_command" may already be in one of the user's PATH elements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top