commons-exec:在系统路径上执行程序?
我正在尝试执行一个程序(具体来说,从 ImageMagick 转换),其父文件夹存在于路径中。因此,当我从命令行运行 convert
时,它会运行该命令。但是,以下操作会失败:
String command = "convert"
CommandLine commandLine = CommandLine.parse(command);
commandLine.addArgument(...)
...
int exitValue = executor.execute(commandLine);
如果我指定转换可执行文件的完整路径 (C:\Program files\...
),则此代码将起作用。如果我不这样做,我会抛出一个异常,退出值为 4
。
如何让 commons-exec 识别系统路径?
I'm trying to execute a program (convert from ImageMagick, to be specific) whose parent folder exists on the path. Ergo, when I run convert
from the command line, it runs the command. The following, however, fails:
String command = "convert"
CommandLine commandLine = CommandLine.parse(command);
commandLine.addArgument(...)
...
int exitValue = executor.execute(commandLine);
If I specify the full path of the convert executable (C:\Program files\...
) then this code works. If I don't do this, I get an exception thrown with exit value 4
.
How do I get commons-exec to recognize the system path?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我之前遇到过这样的问题,系统设置的 PATH 不是 java 进程所看到的。作为调试此问题的一种方法,您可以使用以下命令打印出 java 进程所看到的路径环境变量:
这将为您提供一个映射,您可以查看 Java 是否可以看到该路径变量。如果它不存在,那么下一步就是找出为什么你看不到它。
如果它在那里,我会尝试运行您的 excutor.execute 命令,如下所示:
I've run into issues like this before where the system set PATH is not what the java process is seeing. As a way to debug this you can print out what the java process sees as the path env variable by using:
Which will give you a map and you can look to see if Java can see the path variable. If it is not there then the next step would be to figure out why you can't see it.
If it is there I would try running your excutor.execute command as follows: