Java commons exec:在系统路径上执行程序?

Java commons exec:在系统路径上执行程序?,java,path,environment-variables,system-calls,apache-commons-exec,Java,Path,Environment Variables,System Calls,Apache Commons Exec,我正在尝试执行一个程序(具体来说,是从ImageMagick转换而来),其父文件夹位于路径上。因此,当我从命令行运行convert时,它会运行命令。但是,以下操作失败: String command = "convert" CommandLine commandLine = CommandLine.parse(command); commandLine.addArgument(...) ... int exitValue = executor.execute(commandLine); 如果我

我正在尝试执行一个程序(具体来说,是从ImageMagick转换而来),其父文件夹位于路径上。因此,当我从命令行运行
convert
时,它会运行命令。但是,以下操作失败:

String command = "convert"
CommandLine commandLine = CommandLine.parse(command);
commandLine.addArgument(...)
...
int exitValue = executor.execute(commandLine);
如果我指定转换可执行文件(
C:\ProgramFiles\…
)的完整路径,则此代码可以工作。如果我不这样做,就会抛出一个异常,其退出值为
4


如何让commons exec识别系统路径?

我以前遇到过这样的问题,系统设置路径不是java进程看到的。作为对此进行调试的一种方法,您可以使用以下命令打印出java进程视为path env变量的内容:

EnvironmentUtils.getProcEnvironment();
这将为您提供一个映射,您可以查看Java是否可以看到path变量。如果它不在那里,那么下一步就是找出为什么你看不到它

如果有,我将尝试运行excutor.execute命令,如下所示:

int exitValue = executor.execute(commandLine, EnvironmentUtils.getProcEnvironment());