我是使用Apache实用程序的Java新手。
我正在使用以下代码研究Apache的DefaultExecutor
方法。
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();
}
}
}
=========================
我的简单问题:上面的代码仅能正常工作,但是当我将字符串重定向到
JTextArea
时,它失败了。我们该怎么做?即如何将ping信息打印到
JTextArea
中?为什么SetText / append不起作用? 最佳答案
我see从Java 7开始setText()
和append()
不再是线程安全的。
关于java - 适用于JTextArea的Apache DefaultExecutor和Ping命令,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27502456/