I'm trying to get the full path of system programs using the following Java code:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class PathExtractor {
public static void main(String[] args) throws Exception {
ProcessBuilder processBuilder = new ProcessBuilder("which", "mvn");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();
process.waitFor();
try (BufferedReader in = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
System.out.println(in.readLine());
}
}
}
When compiled and run from the command line, the expected output is printed out:
/usr/local/bin/mvn
However, when run within Eclipse, I get exit value 1 with the following output:
null
After a bit of research, I found a similar question, with this answer suggesting to start a shell to run the previous command:
new ProcessBuilder("/bin/sh", "-c", "which mvn")
Unfortunately the command still fails with a null
output. Note that on a Windows machine simply using new ProcessBuilder("where", "mvn") has the desired effect.
How can I determine the path of programs programmatically from within Eclipse?