如何通过 eclipse-plugin 读取控制台输出文本?

How to read console-output-text by an eclipse-plugin?

我正在尝试编写一个 Eclipse 插件,当已经 运行 LaunchConfiguration 在控制台中打印用户预定义的字符串时,它会启动 LaunchConfiguration

这是我正在寻找的伪代码示例:

            String check = "Server started and running";

            new ConsoleOutputListener(Event event) {
            String consoleText = event.getConsoleOutputTextOfAllConsoles();

                if(consoleText.contains(check)) {
                    //launch LaunchConfiguration 
                }

            }

有没有办法实现上面的例子? 任何帮助我一如既往地感激!

你的问题有点不清楚你到底想做什么,但你可以使用 org.eclipse.ui.console.consolePatternMatchListeners 扩展点在控制台上定义模式匹配侦听器。

类似于(来自 Eclipse 帮助):

<extension
     point="org.eclipse.ui.console.consolePatternMatchListeners">
  <consolePatternMatchListener
        class="com.example.ExampleConsolePatternMatcher"
        id="com.example.ExampleConsolePatternMatcher"
        regex=".*foo.*">
     <enablement>
        <test property="org.eclipse.ui.console.consoleTypeTest" value="exampleConsole"/>
     </enablement>
  </consolePatternMatchListener>
</extension>

匹配特定类型控制台上的正则表达式。

控制台支持有许多扩展点,用于定义不同类型的控制台扩展。

如果您想更动态地执行此操作,您可以使用 IConsoleManager:

监听正在创建的控制台
IConsoleManager manager = ConsolePlugin.getDefault().getConsoleManager(); 

// Existing consoles
IConsole[] consoles = manager.getConsoles();

// Listen for consoles being added/removed
manager.addConsoleListener(console listener);

文本控制台将是 TextConsole 的实例,您可以使用 TextConsole addPatternMatchListener 方法添加模式侦听器。