in the other thread we were trying to get an InputStream(and err) and OutputStream connected to a separate process...right? As I recall we never managed to get the outputstream connected/working and we just dropped it in favor of one-time calls.
Well, I figured it out finally...
class ShellAccess {
Process whatever;
public BufferedWriter doCommand(String cmd) throws IOException {
Runtime r = Runtime.getRuntime();
Process p = r.exec(cmd);
set(p); //connect our handle.
System.setIn(p.getInputStream());
//you can 'grab' the error stream too
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(p.getOutputStream()));
return out;
}
//so we can get a handle on the Process object to destroy() it later
public void set(Process p) { whatever = p; }
public Process get() { return whatever; }
}
//in a separate thread we can monitor System.in like so:
public void run() {
try {
while (true) {
if (System.in.available() != 0) {
byte[] b = new byte[System.in.available()];
System.in.read(b, 0, System.in.available());
String s = new String(b).trim();
//do something with "s".
}
}
} catch (Exception ex) {/*do something meaningful*/ }
}
so to use you'd do something like..
ShellAccess access = new ShellAccess();
BufferedWriter myout = access.doCommand("thecommand");
Process ourprocess = access.get();
if (myout != null) {
//it worked.
out.write("something");
out.flush();
out.close();
ourprocess.destroy();
}
..all in a try { } catch() of course.
I tried several similar ways of doing it but only that way worked. Hope it helps