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

发布于 2024-08-30 02:22:17 字数 437 浏览 4 评论 0原文

我正在尝试执行一个程序(具体来说,从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

长伴 2024-09-06 02:22:17

我之前遇到过这样的问题,系统设置的 PATH 不是 java 进程所看到的。作为调试此问题的一种方法,您可以使用以下命令打印出 java 进程所看到的路径环境变量:

EnvironmentUtils.getProcEnvironment();

这将为您提供一个映射,您可以查看 Java 是否可以看到该路径变量。如果它不存在,那么下一步就是找出为什么你看不到它。

如果它在那里,我会尝试运行您的 excutor.execute 命令,如下所示:

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

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:

EnvironmentUtils.getProcEnvironment();

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:

int exitValue = executor.execute(commandLine, EnvironmentUtils.getProcEnvironment());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文