Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- From: Frank D. Greco <fgreco@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 05 Mar 2009 21:58:52 -0500
Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx> sez:
Frank D. Greco wrote:
Knute Johnson <nospam@xxxxxxxxxxxxxxxxxxxxxxx> sez:
Frank D. Greco wrote:
Is posting code here ok? I narrowed the problem to a short-ish
hello-world app. I don't want to ruffle the e-feathers here. ;)
Actually, the real code or an SSCCE is a real help in diagnoses. So
post the code :-).
Ok. Here's my hello-world that shows the problem. There are three
classes here. The third one is trivial (it just sends stuff to stdout/stderr).
The first class is the driver; its responsible for setting up the Process and
execing. The 2nd class is what confuses me.
Thanks... f
To run the working code:
% java JtaCommandRunner java Dummy 100
To run the non-working code:
% java -Dinvokelater JtaCommandRunner java Dummy 100
# - JtaCommandRunner.java -------------------------------------
import java.io.*;
import java.awt.*;
import javax.swing.*;
public class JtaCommandRunner {
private static JTextArea jta;
private static int returnValue = Integer.MIN_VALUE;
// ******************************************************
public JtaCommandRunner(JTextArea jta) {
this.jta = jta;
}
// ******************************************************
public void run(String[] args) {
try {
Process pro = null;
if (args.length > 1) {
pro = Runtime.getRuntime().exec(args);
} else {
pro = Runtime.getRuntime().exec(args[0]);
}
InputStream error = pro.getErrorStream();
InputStream output = pro.getInputStream();
Thread err = new Thread(new JtaOutReader(jta, error));
Thread out = new Thread(new JtaOutReader(jta, output));
out.start();
err.start();
returnValue = pro.waitFor();
} catch (java.io.IOException e) {
e.printStackTrace();
} catch (java.lang.InterruptedException e) {
e.printStackTrace();
}
}
// ******************************************************
public static void main(String[] args) {
if (args.length < 1) {
System.out.println("Usage: java CommandRunner <command string>");
System.exit(-1);
}
JFrame f = new JFrame("CommandRunner");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLayout(new BorderLayout());
JScrollPane jsp = new JScrollPane( jta = new JTextArea(50, 50) );
jta.setFont(new Font("Lucida Sans Typewriter", Font.PLAIN, 12));
f.add(jsp, BorderLayout.CENTER);
f.pack();
f.setVisible(true);
new JtaCommandRunner(jta).run(args);
}
}
# - JtaOutReader.java -------------------------------------
import javax.swing.*;
import java.io.*;
public class JtaOutReader implements Runnable {
private JTextArea jta;
private InputStream is;
private String temp = null;
public JtaOutReader(JTextArea jta, InputStream is) {
this.is = is;
this.jta = jta;
}
public void run() {
try {
BufferedReader in = new BufferedReader(new InputStreamReader(is));
if ( System.getProperty("invokelater") == null ) {
// This works
while ((temp = in.readLine()) != null) {
jta.append(temp+"\n");
}
} else {
// This doesn't
while ((temp = in.readLine()) != null) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
jta.append(temp+"\n");
}
});
}
}
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
# - Dummy.java (just sends stuff to stdout/stderr)-------------------------
public class Dummy {
public static void main(String[] argv) {
int count = argv.length == 0 ? 50 : Integer.parseInt(argv[0]);
for (int i = 0; i < count; i++) {
System.out.println("STDOUT: " + i);
System.err.println(" STDERR: " + i);
}
}
}
.
- Follow-Ups:
- References:
- Capturing both stdout and stderr of exec-ed process in JTextArea
- From: Frank D . Greco
- Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- From: Knute Johnson
- Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- From: Frank D . Greco
- Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- From: Knute Johnson
- Capturing both stdout and stderr of exec-ed process in JTextArea
- Prev by Date: Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- Next by Date: Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- Previous by thread: Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- Next by thread: Re: Capturing both stdout and stderr of exec-ed process in JTextArea
- Index(es):
Relevant Pages
|