问题
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?
回答1:
I think you need to set the PATH as an environment variable in your Eclipse Run configuration.
回答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-
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();
回答3:
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%
回答4:
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.
回答5:
You can fully qualify the location of gs--that's probably the best way since you shouldn't be trusting the system's path...
来源:https://stackoverflow.com/questions/2610469/java-runtime-command-line-process