Java: 用自己的终端启动子进程
Java: Start sub process with own terminal
我有两个控制器和一个启动它们的程序。
一个是生成数据的模拟器,另一个是分析数据。
这两个控制器相互依赖并使用 RMI 进行通信。
因此,我在另一个线程中启动模拟器,在我的主线程中启动分析器。
效果很好。
现在的问题是,它们都在控制台上产生了相当多的输出,我真的希望它们能打印到两个不同的终端。
有办法吗?
我尝试将模拟器作为子进程启动在一个新的命令行中(平台独立性将是下一步)
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());
}
}
Is it necessary to output the messages to these consoles? If not, you could simply implement a class (say, OutputWindow) that takes messages in some way and shows them in the window.
为了更好地说明我在评论中提出的观点,请考虑以下 OutputWindow
的实现:
class OutputWindow implements Runnable {
private String title;
private JFrame frame;
private JTextArea outputArea;
public OutputWindow(String title) {
this.title = title;
this.outputArea = new JTextArea(15, 50);
this.outputArea.setEditable(false);
}
@Override
public void run() {
frame = new JFrame(this.title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outputPanel = new JPanel(new FlowLayout());
outputPanel.add(outputArea);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public void println(String msg) {
outputArea.append(msg + "\n");
}
public void println(Throwable t) {
this.println(t.toString());
}
public void print(String msg) {
outputArea.append(msg);
}
public void printStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
this.println(sw.toString());
}
}
一段简单且经过快速测试的代码。一个人会像这样打开一个新的 "console":
OutputWindow console = new OutputWindow("Console");
SwingUtilities.invokeLater(console);
你可以试试看:
OutputWindow console = new OutputWindow("Console");
SwingUtilities.invokeLater(console);
try {
System.out.println(2 / 0); // just to throw an exception
} catch (ArithmeticException ex) {
console.printStackTrace(ex);
}
这样做的好处是您可以自定义它的程度!无限可能。使用超棒的颜色和您想要的任何类型的功能创建您自己的最先进的控制台。
这里最简单的解决方案是将输出写入文件,手动打开终端并启动阻塞 tail
我有两个控制器和一个启动它们的程序。 一个是生成数据的模拟器,另一个是分析数据。 这两个控制器相互依赖并使用 RMI 进行通信。 因此,我在另一个线程中启动模拟器,在我的主线程中启动分析器。 效果很好。
现在的问题是,它们都在控制台上产生了相当多的输出,我真的希望它们能打印到两个不同的终端。 有办法吗?
我尝试将模拟器作为子进程启动在一个新的命令行中(平台独立性将是下一步)
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());
}
}
Is it necessary to output the messages to these consoles? If not, you could simply implement a class (say, OutputWindow) that takes messages in some way and shows them in the window.
为了更好地说明我在评论中提出的观点,请考虑以下 OutputWindow
的实现:
class OutputWindow implements Runnable {
private String title;
private JFrame frame;
private JTextArea outputArea;
public OutputWindow(String title) {
this.title = title;
this.outputArea = new JTextArea(15, 50);
this.outputArea.setEditable(false);
}
@Override
public void run() {
frame = new JFrame(this.title);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel outputPanel = new JPanel(new FlowLayout());
outputPanel.add(outputArea);
frame.add(outputPanel);
frame.pack();
frame.setVisible(true);
}
public void println(String msg) {
outputArea.append(msg + "\n");
}
public void println(Throwable t) {
this.println(t.toString());
}
public void print(String msg) {
outputArea.append(msg);
}
public void printStackTrace(Throwable t) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
t.printStackTrace(pw);
this.println(sw.toString());
}
}
一段简单且经过快速测试的代码。一个人会像这样打开一个新的 "console":
OutputWindow console = new OutputWindow("Console");
SwingUtilities.invokeLater(console);
你可以试试看:
OutputWindow console = new OutputWindow("Console");
SwingUtilities.invokeLater(console);
try {
System.out.println(2 / 0); // just to throw an exception
} catch (ArithmeticException ex) {
console.printStackTrace(ex);
}
这样做的好处是您可以自定义它的程度!无限可能。使用超棒的颜色和您想要的任何类型的功能创建您自己的最先进的控制台。
这里最简单的解决方案是将输出写入文件,手动打开终端并启动阻塞 tail