Java ProcessBuilder: environment set correctly but still command not found

前端 未结 1 952
[愿得一人]
[愿得一人] 2021-01-22 14:07

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 prog

相关标签:
1条回答
  • 2021-01-22 15:04

    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.

    0 讨论(0)
提交回复
热议问题