views:

291

answers:

3

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  A: 

I think you need to set the PATH as an environment variable in your Eclipse Run configuration.

Ken Liu
So I've tried adding the path to "gs" in my "Run Configurations" -> Environment Tab and "Linked Resources" (Preferences -> General -> Workspace -> Linked Resources). Neither has worked...
AEIOU
A: 

You can fully qualify the location of gs--that's probably the best way since you shouldn't be trusting the system's path...

Bill K
+1  A: 

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();
Erik B