4

I'm trying to run a program which is in my system path using ProcessBuilder however at the moment I'm having to specify the full path to the executable.

Process p1 = new ProcessBuilder().inheritIO().command("mycommand","myarg").start();
Reader rdr = new InputStreamReader(p1.getInputStream());
StringBuilder sb = new StringBuilder();
for(int i; (i = rdr.read()) !=-1;) {
     sb.append((char)i);
}
String var = sb.toString();
System.out.println(var);

I can run $mycommand myarg from the bash terminal but if I try this in Java I get a file not found error. Is there any way to get this path in Java or to get Java to look in the system path for the executable? I need this to run on multiple machines where the executable will be in different places.

Lii
  • 11,553
  • 8
  • 64
  • 88
user2086359
  • 325
  • 3
  • 13
  • Possible duplicate of [How to set PATH environment variable in ProcessBuilder java in windows](https://stackoverflow.com/questions/26992165/how-to-set-path-environment-variable-in-processbuilder-java-in-windows) – dpr Aug 17 '17 at 13:27

0 Answers0