2 つのコントローラーと、両方を開始する 1 つのプログラムがあります。1 つはデータを生成するエミュレーターで、もう 1 つはデータを分析します。2 つのコントローラーは相互に依存し、RMI を使用して通信します。したがって、別のスレッドでエミュレーターを開始し、メイン スレッドでアナライザーを開始します。 それは完全にうまくいきます。
問題は、どちらもコンソールにかなりの量の出力を生成することであり、2 つの異なる端末に出力することを本当に望んでいます。それを行う方法はありますか?
新しいコマンド ライン内のサブ プロセスとしてエミュレーターを起動しようとしました(プラットフォームに依存しないことが次のステップになります)。
String separator = System.getProperty("file.separator");
String classpath = System.getProperty("java.class.path");
String path = System.getProperty("java.home")
+ separator + "bin" + separator + "java";
ProcessBuilder processBuilder =
new ProcessBuilder(
"\"" + path + "\"",
"-cp",
classpath,
TheEumlator.class.getName());
String command = StringUtils.join(processBuilder.command(), " ");
Process p = Runtime.getRuntime().exec(String.format("cmd /c start cmd.exe /K %s", command));
ただし、classpath
が長すぎて、の出力cmd.exe
はThe command line is too long.
独自の出力端子を持つ別のスレッドまたはプロセスを生成する方法を知っていますか? 何か提案をいただければ幸いです。
乾杯
アップデート
OlaviMustanoja の回答をこのソリューションと組み合わせました http://unserializableone.blogspot.co.uk/2009/01/redirecting-systemout-and-systemerr-to.html
標準System.out
、System.err
およびスタック トレースを使用するようになりました。また、スクロールします。
public class ConsoleWindow implements Runnable {
private String title;
private JFrame frame;
private JTextArea outputArea;
private JScrollPane scrollPane;
public ConsoleWindow(String title, boolean redirectStreams) {
this.title = title;
this.outputArea = new JTextArea(30, 80);
this.outputArea.setEditable(false);
if (redirectStreams)
redirectSystemStreams();
}
public ConsoleWindow(String title) {
this(title, false);
}
@Override
public void run() {
frame = new JFrame(this.title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
scrollPane = new JScrollPane(outputArea);
JPanel outputPanel = new JPanel(new FlowLayout());
outputPanel.add(scrollPane);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
private void updateTextArea(final String text) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
outputArea.append(text);
}
});
}
private void redirectSystemStreams() {
OutputStream out = new OutputStream() {
@Override
public void write(int b) throws IOException {
updateTextArea(String.valueOf((char) b));
}
@Override
public void write(byte[] b, int off, int len) throws IOException {
updateTextArea(new String(b, off, len));
}
@Override
public void write(byte[] b) throws IOException {
write(b, 0, b.length);
}
};
System.setOut(new PrintStream(out, true));
System.setErr(new PrintStream(out, true));
}
public void println(String msg) {
updateTextArea(msg + "\n");
}
public void println(Throwable t) {
println(t.toString());
}
public void print(String msg) {
updateTextArea(msg);
}
public void printStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
this.println(sw.toString());
}
}