Java运行时命令行进程
我有一个包含以下代码的类:
Process process = null;
try {
process = Runtime.getRuntime().exec("gs -version");
System.out.println(process.toString());
} catch (Exception e1) {
e1.printStackTrace();
} finally {
process.destroy();
}
我可以在命令行上运行“gs -version”并获取: GPL Ghostscript 8.71 (2010-02-10) 版权所有 (C) 2010 Artifex Software, Inc. 保留所有权利。
所以我知道我至少在某处设置了路径。
我可以从命令行运行该类并且它可以工作。但是当我使用 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
在我的程序中,我可以将“gs”替换为:“java”、“mvn”、“svn”并且它可以工作。但“gs”则不然。只有在日食中我才遇到这个问题。
关于我需要做什么来解决这个问题有什么想法吗?
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.
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:
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. But "gs" does not. It's only in eclipse I have this problem.
Any ideas, on what I need to do to resolve this issue?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我认为您需要在 Eclipse Run 配置中将 PATH 设置为环境变量。
I think you need to set the PATH as an environment variable in your Eclipse Run configuration.
(参见http://www.devdaily.com/java/java- exec-processbuilder-process-2 对于获取此代码片段的文章,您需要其中的其他类才能使其工作。)
试一试 -
(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.)
Give this a shot-
在程序的 Eclipse 运行配置中,转到环境选项卡并添加一个名为“PATH”的新环境变量,其中值类似于以下内容(在 Windows 中)“C:\Program Files (x86)\gs\gs9.02 \bin;%PATH%"。
这应该有效。
或者在你的java程序中,不要执行 Runtime.exec("gs..."),而是执行 Runtime.exec("my-batch-file.bat"...) 其中 my-batch-file .bat 将包含一行设置 Ghostscript 可执行文件的路径:
set PATH=C:\Program Files (x86)\gs\gs9.02\bin;%PATH%
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%".
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:
set PATH=C:\Program Files (x86)\gs\gs9.02\bin;%PATH%
我有同样的问题,我发现了问题。 Eclipse 中的路径变量的内容与命令行中的路径变量的内容不同。
解决方案:
在命令行中查找 $Path 变量并复制内容。
然后打开运行配置->环境并选择新建。
名称:$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.
Solution:
Look up for the $Path variable in command Line and copy the content.
Then open Run Configuration->Environment and select new.
Name: $PATH
Value: insert the copied content.
That solved the Problem.
您可以完全限定 gs 的位置——这可能是最好的方法,因为您不应该信任系统的路径......
You can fully qualify the location of gs--that's probably the best way since you shouldn't be trusting the system's path...