[英]Cross-platform way to run external process from java?
I need to call an external program from Java such as ImageMagick's convert
. 我需要从Java调用外部程序,如ImageMagick的convert
。 It fails to work on Windows unless I put cmd /c
before the actual command. 除非我在实际命令之前放置cmd /c
,否则它无法在Windows上运行。
String source = "test.jpg";
String result = "test-thumbnail.jpg";
ProcessBuilder builder = new ProcessBuilder().command("cmd", "/c", "convert", source, "-thumbnail", "295x", result);
Process process = builder.start();
How to avoid using cmd /c
so that my code works on OS other than Windows? 如何避免使用cmd /c
使我的代码在Windows以外的操作系统上运行?
Without cmd /c
I get into very similar problem as described here: running imagemagick convert (console application) from python - that there exists a native Windows convert.exe
which is being called rather than ImageMagick's convert.exe
. 没有cmd /c
我遇到了如下所述的非常类似的问题: 从python运行imagemagick convert(控制台应用程序) - 存在一个本机Windows convert.exe
被调用而不是ImageMagick的convert.exe
。 It seems like PATH
is not picked by environment of the child process. 似乎PATH
不被子进程的环境挑选。
I have double checked that my system PATH
has ImageMagick directory before C:\\Windows\\system32
. 我已经仔细检查过我的系统PATH
在C:\\Windows\\system32
之前有ImageMagick目录。 Also the command itself runs perfectly fine when I type it into the Windows command line. 当我在Windows命令行中输入命令时,命令本身也可以正常运行。
你可以查看操作系统。
private static String OS = System.getProperty("os.name")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.