4

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.exeThe 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());
    }
}
4

2 に答える 2

1

これらのコンソールにメッセージを出力する必要がありますか? そうでない場合は、何らかの方法でメッセージを取得してウィンドウに表示するクラス (たとえば、OutputWindow) を実装するだけです。

私のコメントで述べた点をよりよく説明するために、次の の実装を検討して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);
}

これを行う利点は、カスタマイズできる範囲です。限りない可能性。非常に素晴らしい色と必要な機能を備えた独自の最先端のコンソールを作成します。

于 2015-01-23T12:47:30.993 に答える