kype
kype

Reputation: 595

Environment variables in Eclipse on OSX

i have imported a certain library which executes the following command

Runtime.getRuntime().exec("svd");

Now in my bash shell, i can execute svd as it points to installed folder "/usr/local/bin/svd". However my java programs are unable to execute "svd" and eclipse returns with error "Cannot run program "svd": error=2, No such file or directory"

I have added the following to my environment variables in run configurations of eclipse.

$PATH = /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/bin/svd
svd = /usr/local/bin/svd

However eclipse still says its unable to run program "svd". Is there any way to fix this other than manually writing the full path?

e.g Runtime.getRuntime().exec("/usr/local/bin/svd");

Upvotes: 3

Views: 2169

Answers (1)

Daniel
Daniel

Reputation: 1861

It's not eclipse who cannot run the svd program but the jvm, because it cannot find svd's path on the system.

You should put your svd program on $PATH variable so that when the JVM runs your program and finds a call to svd, it should know where this svd program is located so it may call it.

For how to configure your $PATH variable on OSX, check here : Setting environment variables in OS X?

I also noticed you use Runtime to run external programs in your java program. That is an ancient way to run external programs in java. You should consider using the ProcessBuilder instead. It's much more flexible, and is considered the best choice to run external programs now:

ProcessBuilder pb = new ProcessBuilder("svd");
Process p = pb.start();
//You could also read the error stream, so that when svd is not correctly set on the running system, you may alert the user.
BufferedReader br = new BufferedReader(new InputStreamReader(p.getErrorStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
    sb.append(line);
}

int retCode = p.waitFor();
if(retCode == 2){
    //alert the user that svd is not correctly set on PATH variable.
    LOGGER.error(sb);
    System.out.println("ERROR!! Could not run svd  because it's not correctly set on PATH variable");
}

Upvotes: 1

Related Questions