XRun
This is a Java library that extends the functionary of java.lang.Process. With this library can you call some process (another application) and read/write to the standard input/ouput in an easy way. No thread is necessary and the process will “never” block your application.
The most difficult issue managing processes is to create and control threads to read and write to the standard input/output of the process. With this library it’s possible to manage the process in a simple way, just calling the read/write procedures and informing the minimum and maximum time to wait.
In the “demo” folder, there is a simple application called “Echo.jar” that can be used for tests. This application just asks for a string and how many times to repeat it. Here is a print screen of this application running.
Echo test program (type 'stop' to end)
Write something: Rock and Roll
How many times to repeat: 1
Rock and Roll
waiting 2141 milisec (max 3000.0)
Write something: stop
Good by!
The java code used to call and interact with the this application in the same way could be:
:::java
(...)
XRun xrun = new XRun();
try {
xrun.run("java -jar Echo.jar");
try {
System.out.println(xrun.readStdOut(2000, 4000));
xrun.write("Rock and Roll");
System.out.println(xrun.readStdOut(1000, 2000));
xrun.write("3");
System.out.println(xrun.readStdOut(3000, 6000));
xrun.write("stop");
System.out.println(xrun.readStdOut(3000, 6000));
} catch (ProcessNotRunning ex) {
System.out.println("Sorry! The application is not running anymore...");
}
} catch (IOException ex) {
System.out.println("Application not found");
} catch (InterruptedException ex) {
System.out.println("Somethig interrupt the process");
} finally {
xrun.destroyAll();
}
(...)
Enjoy!