Java Runtime Process Command Line

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 that I have a path, at least somewhere established.

I can run this class from the 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 is only in the eclipse. I have this problem.

Any ideas what I need to do to solve this problem?

+3
5

, PATH Eclipse.

+3

( http://www.devdaily.com/java/java-exec-processbuilder-process-2 , , , .)

-

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

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:

PATH = C:\Program Files (x86)\gs\gs9.02\bin;% PATH%

+2

, . Eclipse , , .

:

$Path . " - > " "". : $PATH : .

.

+1

You can completely determine the location of gs - this is probably the best way, since you should not trust the system path ...

-1
source

Source: https://habr.com/ru/post/1740515/


All Articles