[英]Java Runtime command line Process
I have a class with the following code: 我有一个包含以下代码的类:
Process process = null;
try {
process = Runtime.getRuntime().exec("gs -version");
System.out.println(process.toString());
} catch (Exception e1) {
e1.printStackTrace();
} finally {
process.destroy();
}
I can run "gs -version" on my command line and get: GPL Ghostscript 8.71 (2010-02-10) Copyright (C) 2010 Artifex Software, Inc. All rights reserved. 我可以在命令行运行“gs -version”并获取:GPL Ghostscript 8.71(2010-02-10)版权所有(C)2010 Artifex Software,Inc。保留所有权利。
So I know I have the path at least set somewhere. 所以我知道我的路径至少要设置在某个地方。
I can run that class from command line and it works. 我可以从命令行运行该类,它可以工作。 But when I run it using eclipse I get the following error: 但是当我使用eclipse运行它时,我收到以下错误:
java.io.IOException: Cannot run program "gs": error=2, No such file or directory
at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
at java.lang.Runtime.exec(Runtime.java:593)
at java.lang.Runtime.exec(Runtime.java:431)
at java.lang.Runtime.exec(Runtime.java:328)
at clris.batchdownloader.TestJDBC.main(TestJDBC.java:17)
Caused by: java.io.IOException: error=2, No such file or directory
at java.lang.UNIXProcess.forkAndExec(Native Method)
at java.lang.UNIXProcess.<init>(UNIXProcess.java:53)
at java.lang.ProcessImpl.start(ProcessImpl.java:91)
at java.lang.ProcessBuilder.start(ProcessBuilder.java:452)
... 4 more
In my program, i can replace "gs" with: "java", "mvn", "svn" and it works. 在我的程序中,我可以用“java”,“mvn”,“svn”替换“gs”,它可以工作。 But "gs" does not. 但“gs”却没有。 It's only in eclipse I have this problem. 只有在日食中我才有这个问题。
Any ideas, on what I need to do to resolve this issue? 有什么想法,我需要做些什么来解决这个问题?
我认为您需要在Eclipse Run配置中将PATH设置为环境变量。
(See http://www.devdaily.com/java/java-exec-processbuilder-process-2 for the article from which this snippet was taken, you'll need the other classes in there to make it work.) (有关从中获取此代码段的文章,请参阅http://www.devdaily.com/java/java-exec-processbuilder-process-2 ,您需要其他类才能使其正常工作。)
Give this a shot- 给这个镜头 -
List<String> commands = new ArrayList<String>();
commands.add("/bin/sh");
commands.add("-c");
commands.add("gs -version");
try
{
ProcessBuilder pb = new ProcessBuilder(commands);
Process process = pb.start();
inputStreamHandler = new ThreadedStreamHandler(
process.getInputStream() );
errorStreamHandler = new ThreadedStreamHandler(
process.getErrorStream());
inputStreamHandler.start();
errorStreamHandler.start();
process.waitFor();
inputStreamHandler.interrupt();
errorStreamHandler.interrupt();
inputStreamHandler.join();
errorStreamHandler.join();
}
catch (IOException e)
{
Log.err(e);
}
catch (InterruptedException e)
{
Log.err(e);
}
StringBuilder stdout = inputStreamHandler.getOutputBuffer();
In your Eclipse Run Configurations for your program, go to the Environment tab and add a new Environment variable called "PATH" where the value is something like this (in Windows) "C:\\Program Files (x86)\\gs\\gs9.02\\bin;%PATH%". 在程序的Eclipse Run Configurations中,转到Environment选项卡并添加一个名为“PATH”的新环境变量,其值如下所示(在Windows中)“C:\\ Program Files(x86)\\ gs \\ gs9.02 \\ BIN;%PATH%”。
This should work. 这应该工作。
Either that or in your java program, instead of doing a Runtime.exec("gs..."), do a Runtime.exec("my-batch-file.bat"...) where the my-batch-file.bat will contain a line setting the path to the ghostscript executable: 要么是在java程序中,要么在java程序中,而不是运行Runtime.exec(“gs ...”),在my-batch-file中运行Runtime.exec(“my-batch-file.bat”...) .bat将包含一行设置ghostscript可执行文件的路径:
set PATH=C:\\Program Files (x86)\\gs\\gs9.02\\bin;%PATH% 设置PATH = C:\\ Program Files(x86)\\ gs \\ gs9.02 \\ bin;%PATH%
I had the same issue and i found the problem. 我有同样的问题,我发现了问题。 The Path Variable in Eclipse had different content than the one from the command Line. Eclipse中的Path变量与命令行中的路径变量的内容不同。
Solution: 解:
Look up for the $Path variable in command Line and copy the content. 在命令行中查找$ Path变量并复制内容。 Then open Run Configuration->Environment and select new. 然后打开Run Configuration-> Environment并选择new。 Name: $PATH Value: insert the copied content. 名称:$ PATH值:插入复制的内容。
That solved the Problem. 这解决了问题。
您可以完全限定gs的位置 - 这可能是最好的方法,因为您不应该信任系统的路径......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.