I am a newbie in Java with Apache utilities.
I am studying Apache's DefaultExecutor
method with the following code.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import org.apache.commons.exec.CommandLine;
import org.apache.commons.exec.DefaultExecuteResultHandler;
import org.apache.commons.exec.DefaultExecutor;
import org.apache.commons.exec.ExecuteException;
import org.apache.commons.exec.LogOutputStream;
import org.apache.commons.exec.PumpStreamHandler;
public class PingExampleApacheExec {
public static void main(String[] args) {
//
CommandLine commandLine = new CommandLine("ping");
commandLine.addArgument("/n");
commandLine.addArgument("5");
commandLine.addArguments("/w 1000");
commandLine.addArgument("127.0.0.1");
// Executor
DefaultExecutor executor = new DefaultExecutor();
try {
// LogOutputStream
LogOutputStream output = new LogOutputStream() {
@Override
protected void processLine(String line, int level) {
// NewJFrame1 myLOG = new NewJFrame1(); // not worked
// myLOG.mainLOG(); // not worked
// myLOG.jTextArea1.setText(line); // not worked
System.out.println(line);
}
};
PumpStreamHandler streamHandler = new PumpStreamHandler(output);
executor.setStreamHandler(streamHandler);
executor.setExitValue(0);
DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
executor.execute(commandLine, resultHandler);
// TODO output.close()
} catch (ExecuteException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
==========================
My simple question: The above code is simply working, but it failed when I redirect the string to a JTextArea
.
How can we do it? i.e. how to print the ping info into a JTextArea
? Why SetText/append didn't work?