gpt4 book ai didi

Java:使用自己的终端启动子进程

转载 作者:搜寻专家 更新时间:2023-11-01 03:37:04 24 4
gpt4 key购买 nike

我有两个 Controller 和一个启动它们的程序。一个是生成数据的模拟器,另一个是分析数据。这两个 Controller 相互依赖并使用 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.outSystem.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());
}

}

一段简单的、经过快速测试的代码。一个人会像这样打开一个新的“控制台”:

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);
}

这样做的好处是您可以自定义它的程度!无限可能。使用超棒的颜色和您想要的任何类型的功能创建您自己的最先进的控制台。

关于Java:使用自己的终端启动子进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28109509/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com