Anyhow, I've had this need to interact with a couple of programs that have no API whatsoever. My solution has been to run the suckers and grab their output. For that reason, I got to learn a bit about how to execute a command in a separate process in Java. Through a class called ProcessBuilder (java.lang package), it allows one to get the input or output stream of the subprocess, as well as the exit status (among other available information).
Quick example:
import java.io.*;
import java.util.*;
public class MyExample {
public static void main(String args[]) throws IOException {
if (args.length <= 0) {
System.err.println("What should I run?");
System.exit(-1);
}
Process process = new ProcessBuilder(args).start();
InputStream is = process.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
BufferedReader buffer = new BufferedReader(reader);
String line;
System.out.printf("Output of running %s is:", Arrays.toString(args));
while ((line = buffer.readLine()) != null) {
System.out.println(line);
}
}
}
And while I won't go into details of how to customize your ProcessBuilder inputs, you can try this out with > java MyExample ls.
My problem is that the software I'm executing in the subprocess, forks other threads and processes. Which screws up the process' signaling in Java. So I actually do the process forking in another C-based software I coded, and use Java to call that one. (Complicated tongue-twister, right?) But, this requires that the Java process waits until my C code finishes, which has to wait until the other programs finishes. For that reason, I had to include a process.waitFor(); right after the start command.
This leads to the title of this post: How to specify a time out value to the waitFor()? Apparently waitFor() has no timeout. Which means that I need to create yet another Thread just to wait for it, and join both of the after the timeout expires.
- Thread thread = new Thread(new Runnable()
- {
- public void run()
- {
- try
- {
- process.waitFor();
- }
- catch (Exception e) { e.printStackTrace(); }
- }
- });
- thread.start();
- thread.join(15000); // 15 seconds
It's one of those things that I'm not necessarily bragging about, but not ashamed either. And most importantly, I don't want to forget. :)
No comments:
Post a Comment