I am trying to use runtime.getruntime().exec(String command) to return a value so that I can make the system decide on executing it again or not. Is there a solution for this?
Runtime.exec returns a Process, process has a method called waitFor, which will wait for the running process to terminate and return.
Process has a method called exitVaue, which returns an int returning the exit state of the program. Convention suggests that 0 is an indication of a normal termination, but this might be contextual to the program you are running.
You would need to...
exitValue returned by the instance of Process against the known valid exit values.Things to note...
InputStreams (input and error) of the Process, as failing to do so can cause some processes to stallProcessBuilder is generally a better solution as has better functionality when it comes to dealing with multiple parameters for the command, has redirection and can even determine the starting directory context for the command...Runtime runtime = Runtime.getRuntime();
Process process = runtime.exec(args);
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With