I'm using the following object, to run a bash command through my Java code.
public class Scripter {
private final String[] ENV = {"PATH=/bin:/usr/bin/"};
private String cmd;
private Process process;
public Scripter(String cmd) throws IOException {
this.cmd = cmd;
process = Runtime.getRuntime().exec(cmd, ENV);
}
}
Now I'm calling this object and trying to print the output in this function.
public static void main(String[] args) {
Scripter gitCheck;
try {
gitCheck = new Scripter("git --version");
} catch (IOException e) {
e.printStackTrace();
}
Scanner sc = new Scanner(System.in);
if (sc.hasNext()) {
System.out.println(sc.next());
}
}
The program goes on an infinite loop and does nothing. What am I doing wrong here?
sc.next(). You need to read aboutProcessBuilderand how to retrieve the input and output streams of the process.System.indoes not magically get attached to the process.