I am trying to read the stdInput and stdError from a process while the process is running. I created a thread to run the process, and that works. In the constructor for the I pass a reference to a Process that is created so I can watch the output from the process. The problem is it does not seem to be linking the reference process leaving it set to null. The goal of all this is to be able to watch the output of the process while the process is still running, currently I am stuck with looking at the output after it is done. I figure this is due to the tread being run but I hope there is a way to do this.
try {
Process p = null;
Thread mythread = new Thread( new SJ(pathF.getText(), fileF.getText(), p, view) ) ;
mythread.start();
BufferedReader stdInput = null;
BufferedReader stdError = null;
String s = null;
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while (mythread.isAlive()) {
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
}
} catch ( IOException e ) {}
class SJ implements Runnable {
String Path = "";
String File = "";
public static viewering view;
Process p = null;
public SJ ( String arg1, String arg2, Process p1, viewering view ) {
Path = arg1;
File = arg2;
p = p1;
}
public void run() {
String[] command = new String[5];
command[0] = "cmd";
command[1] = "/C";
command[2] = "compile";
command[3] = Path;
command[4] = File;
BufferedReader stdInput = null;
BufferedReader stdError = null;
String s = null;
try {
p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
} catch ( IOException e ) {}
}
}
Here is the code for the final working scenario
public void actionPerformed(ActionEvent ae) {
frame.setVisible( false );
final Thread mythread = new Thread( new SJ(pathF.getText(), fileF.getText(), view) ) ;
mythread.start();
final Timer ThreadTimer = new Timer( 1000, null );
ThreadTimer.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
if (!mythread.isAlive()) {
frame.setVisible( true );
ThreadTimer.stop();
}
}});
ThreadTimer.start();
}
class SJ implements Runnable {
String Path = "";
String File = "";
public static viewering view;
public SJ ( String arg1, String arg2, viewering view1 ) {
Path = arg1;
File = arg2;
view = view1;
}
public void run() {
String[] command = new String[5];
command[0] = "cmd";
command[1] = "/C";
command[2] = "compile";
command[3] = Path;
command[4] = File;
BufferedReader stdInput = null;
BufferedReader stdError = null;
Process p = null;
String s = null;
try {
p = Runtime.getRuntime().exec( command, null, new File( "C:/Users/michael.b.goff/Documents/java/" ) );
stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
stdError = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((s = stdInput.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
while ((s = stdError.readLine()) != null) { System.out.println(s); view.write( s + "\r\n" ); }
} catch ( IOException e ) {}
}
}