【问题标题】:Eclipse crashes on the user inputEclipse 在用户输入时崩溃
【发布时间】:2013-06-14 02:10:33
【问题描述】:

我有一个类“一”,它使用命令编译类“二”

我用这段代码运行两个

Process p2 = Runtime.getRuntime().exec("java two");
           BufferedReader in = new BufferedReader(
                   new InputStreamReader(p2.getInputStream()) );
           while ((line = in.readLine()) != null) {
             System.out.println(line);
           }
           in.close();

现在,当“two”在其主要方法中打印时,它可以正常工作并且它们会打印在控制台中,但是当它有用户输入时,Eclipse 会崩溃。 当我什至删除 while 循环时,它不允许我在控制台中写入

我正在使用

创建一个新控制台
MessageConsole console = new MessageConsole("", null);
    console.activate();
    ConsolePlugin.getDefault().getConsoleManager()
            .addConsoles(new IConsole[] { console });
    MessageConsoleStream stream = console.newMessageStream();
    System.setOut(new PrintStream(stream, true));

【问题讨论】:

  • 请添加更多代码和您的 eclipse 版本,以便我们更好地了解这一切是如何调用的

标签: java eclipse console command


【解决方案1】:

我遇到了类似的问题。我扩展了 MessageConsole(只是为了能够拥有一个特定的 consolePageParticipant),并在构造函数中将 System.out 重定向到一个新的 MessageConsoleStream。使用我的代码的第一个版本时,RCP 应用程序崩溃了,而第二个版本则挂起。

我已经不记得崩溃/挂起的代码是什么样子了,但我发现我无法在显示 MessageConsole 之前更快地重定向输出。所以我使用了一个新线程在重定向之前等待了一段时间(5 秒 - 可能太多了?)。

messageConsoleStream = myConsole.newMessageStream();

new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
        }
        OutputStream out = new OutputStream() {
            @Override
            public void write(int b) throws IOException {
                messageConsoleStream.write(b);
                oldOut.write(b);
            }
        };
        System.setOut(new PrintStream(out));
        LOGGER.debug("'System.out' is redirected to the console."); //$NON-NLS-1$
    }
}, "Redirect system out to console...").start(); //$NON-NLS-1$

仍然最好更改 Thread.sleep(5000);等到控制台显示出来……

【讨论】:

    【解决方案2】:

    在该行中指定终端: 进程 p2 = Runtime.getRuntime().exec("gnome-terminal -x java 二"); 要么 进程 p2 = Runtime.getRuntime().exec("xterm -x java 二");

    这会使程序在前台运行,否则它会变成一个不可见的进程...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-08
      • 2016-03-06
      • 2013-09-22
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多