写入 Eclipse 控制台

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/720963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 13:38:43  来源:igfitidea点击:

Writing to the Eclipse console

javaeclipseeclipse-pluginconsole

提问by

My plugin has to write to the eclipse console - for testing purpose I simplified my code, so that I only have the following:

我的插件必须写入 eclipse 控制台 - 出于测试目的,我简化了我的代码,因此我只有以下内容:

public void start(BundleContext context) throws Exception {
        super.start(context);
        plugin = this;
        System.out.println("Tecomp Plugin is running");

        MessageConsole myConsole = new MessageConsole("My Console", null);
        //myConsole.activate();
        ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[]{ myConsole });
        ConsolePlugin.getDefault().getConsoleManager().showConsoleView(myConsole);
        final MessageConsoleStream stream = myConsole.newMessageStream();
        stream.setActivateOnWrite(true);
        stream.println("Hi there!");
        stream.close();
    }

It's a simple plugin, the method start belogs to the class extending AbstractUIPlugin - it't no rcp application. Code works fine inside the runtime workbench - once the plugin is installed, the output gets lost somewhere. The rest of the plugin is working properly.

这是一个简单的插件,方法 start 属于扩展 AbstractUIPlugin 的类 - 它不是没有 rcp 应用程序。代码在运行时工作台内运行良好 - 一旦安装了插件,输出就会在某处丢失。插件的其余部分工作正常。

Is there perhaps a problem with the ConsolePlugin? Or are streams handled differently in the runtime workbench and the development workbench?

ConsolePlugin 可能有问题吗?或者流在运行时工作台和开发工作台中的处理方式不同吗?

I tried both - a feature project and directly copying the plugin jar to the eclipse directory - for installing the plugin - same result for both...

我尝试了两者 - 一个功能项目并直接将插件 jar 复制到 eclipse 目录 - 用于安装插件 - 两者的结果相同......

Any help is welcome, because I'm struggeling with that problem for a while now...

欢迎任何帮助,因为我现在正在努力解决这个问题一段时间......

Regards, Kathi

问候, 凯西

Edit:

编辑:

it doesn't seem to be a problem of the Console... I provided my own view for printing the output, but although declard in the plugin.xml there is no view after installing the plugin... here is what I did: -export the plugin with the ExportWizard into a jar-archive -copied this archive to /usr/share/eclipse/plugins -restarted eclipse

它似乎不是控制台的问题......我提供了我自己的打印输出视图,但是虽然在 plugin.xml 中声明了安装插件后没有视图......这是我所做的: - 使用 ExportWizard 将插件导出到 jar-archive - 将此存档复制到 /usr/share/eclipse/plugins -restarted eclipse

or with a feature project: -exported the feature-project containing my plugin with the ExportWizard -removed the above jar archive from the eclipse dir -installed the feature -restarted eclipse

或使用功能项目: - 使用 ExportWizard 导出包含我的插件的功能项目 - 从 eclipse 目录中删除上述 jar 存档 - 安装功能 - 重新启动 Eclipse

both didn't word - did I get something wrong with installing the plugin? The jar-archive is ok, I checked it - it's the latest version. But somehow it seems that eclipse is still working with some older plugin without the changes I made

两者都没有说 - 安装插件时我有什么问题吗?jar-archive 没问题,我检查了它 - 它是最新版本。但不知何故,eclipse 似乎仍在使用一些较旧的插件,而没有我所做的更改

regards, Kathi

问候, 凯西

Edit:

编辑:

I implemented the IStartup Interface end extended the org.eclipse.ui.startup point, but nothing changed... I really thinks it's an installation problem somehow. I commented out some output but it's still printed to the debug console. Is there some sort of plugin cache in eclipse? So that the new code doesn't get read?

我实现了 IStartup 接口端扩展了 org.eclipse.ui.startup 点,但没有任何改变......我真的认为这是一个安装问题。我注释掉了一些输出,但它仍然打印到调试控制台。eclipse 中是否有某种插件缓存?这样新代码就不会被读取?

Edit:

编辑:

Thanks for the suggestions, but starting eclipse with the -clean option didn't help - I'll try to install the plugin in some other environment next week - perhaps there is something wrong with mine...

感谢您的建议,但使用 -clean 选项启动 eclipse 并没有帮助——下周我将尝试在其他环境中安装插件——也许我的有问题......

Edit:

编辑:

the code in the class calling the compilier looks as follows:

调用编译器的类中的代码如下所示:

private MessageConsole findConsole(String name){
  ConsolePlugin plugin = ConsolePlugin.getDefault();
  IConsoleManager conMan = plugin.getConsoleManager();
  IConsole[] existing = conMan.getConsoles();
  for (int i = 0; i<existing.length; i++){
    if (name.equals(existing[i].getName())){
      return (MessageConsole)existing[i];
    }
  }
  //no console found -> create new one
  MessageConsole newConsole = new MessageConsole(name, null);
  conMan.addConsoles(new IConsole[]{newConsole});
  return newConsole;
}



public void run() {
  MessageConsole console = findConsole("tecompConsole");
  //display the tecomp Console
  IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
  String id = IConsoleConstants.ID_CONSOLE_VIEW;
  try {
    IConsoleView view = (IConsoleView) page.showView(id);
    view.display(console);
  } catch (PartInitException e) {
    e.printStackTrace();
  }
  MessageConsoleStream output = console.newMessageStream();
  String tecompPath = TecompPlugin.getDefault().getPreferenceStore().getString(IEiffelConstants.TECOMP_PATH);
  if (checkTecompPath(tecompPath)){
    String line;
    String[] cmd = {tecompPath, pathToAceFile};
    try{
      output.println("calling tecomp");
      Process tecomp = Runtime.getRuntime().exec(cmd);
      //capture stdout und stderr from tecomp
      BufferedReader input = new BufferedReader(
          new InputStreamReader(tecomp.getInputStream()));
      BufferedReader err = new BufferedReader(
          new InputStreamReader(tecomp.getErrorStream()));
      while ((line = input.readLine()) != null ){
        output.println(line);
      }  
      input.close();
      while ((line = err.readLine()) != null){
        output.println(line);
      }
      err.close();
      output.close();
      tecomp.waitFor();
      //System.out.println(tecomp.exitValue());
    }catch (Exception err){
      err.printStackTrace();
    }
  } else {
    try{
    output.println("please specify a tecomp path");
    output.close();
    }catch (Exception err){}
  }
}

but the first test example should work, shouldn't it? I create a new MessageConsoleStream and write to it manually. Thats exactly like the examples I found.

但是第一个测试示例应该可以工作,不是吗?我创建了一个新的 MessageConsoleStream 并手动写入。这和我找到的例子一模一样。

Edit:

编辑:

The code doesn't belog to an rcp application - it's just a plugin, extending the eclipse ide with support for the eiffel programming language. So I think your suggestion doesn't work for my plugin - VonC, at least I don't know where to put your code... My first sample code in the initial question gets called inside the Plugin class extending AbstractUIPlugin within the start(BundleContext context) method. My plugin in running, so somewhere this method gets called. And as I mentioned - plugin works fine inside the runtime workbench...

代码不属于 rcp 应用程序 - 它只是一个插件,扩展了 eclipse ide 并支持 eiffel 编程语言。所以我认为你的建议不适用于我的插件 - VonC,至少我不知道把你的代码放在哪里......我在初始问题中的第一个示例代码在插件类中调用,在 start( BundleContext 上下文)方法。我的插件正在运行,所以在某个地方调用了这个方法。正如我所提到的 - 插件在运行时工作台内工作正常......

I'm sorry if this is not the right place for an additional explanation of my question - but it seemed to be the only place the system allows me to post some further lines. Comments to your answers are not allowed for me, because I just signed in and for that I don't have enough reputation points... so, please correct me, if I'm using the system wrong :) thx

如果这不是对我的问题进行额外解释的正确位置,我很抱歉 - 但它似乎是系统允许我发布更多行的唯一位置。我不允许对您的答案发表评论,因为我刚刚登录,因此我没有足够的声望点...所以,如果我使用的系统有误,请纠正我:) thx

回答by VonC

Did you check "Displaying the console in your RCP application"?

您是否选中了在 RCP 应用程序中显示控制台

Would the follwoing small code (done in the Application.java run()before creating and running the workbench) begin to at least behave like what you want ?

下面的小代码(Application.java run()在创建和运行工作台之前完成)是否会开始至少表现得像你想要的那样?

MessageConsole console = new MessageConsole(”System Output”, null);
ConsolePlugin.getDefault().getConsoleManager().addConsoles(new IConsole[] { console });
ConsolePlugin.getDefault().getConsoleManager().showConsoleView(console);
MessageConsoleStream stream = console.newMessageStream();

System.setOut(new PrintStream(stream));
System.setErr(new PrintStream(stream));

logger = LoggerFactory.getLogger(Application.class); // Previously declared.

(I.e. does a console display itself when your plugin is installed in eclipse ?)

(即,当您的插件安装在 eclipse 中时,控制台会显示自己吗?)

回答by Mario Ortegón

I think the problem is that your plugin is not being started. The code you show won't be activated until some other plug-in tries to access something within your plugin. It seems like this is the case with your debug launch configuration, but not with the packaged application.

我认为问题在于您的插件没有启动。在其他插件尝试访问您插件中的某些内容之前,您显示的代码不会被激活。您的调试启动配置似乎是这种情况,但打包的应用程序则不然。

You could try to implement the IStartup interface, and use the org.eclipse.ui.startup extension point in your plugin to force the initialization as soon as the UI is loaded. This should activate your plugin and execute the console code that you have.

您可以尝试实现 IStartup 接口,并在您的插件中使用 org.eclipse.ui.startup 扩展点以在 UI 加载后立即强制初始化。这应该会激活您的插件并执行您拥有的控制台代码。

Can you maybe add some logging statement or breakpoint in the start() method to ensure that it is being invoked on your deployed application?

您能否在 start() 方法中添加一些日志语句或断点,以确保它在您部署的应用程序上被调用?

回答by gcastro

You probably have to clear the cached data used by OSGI and the eclipse runtime. You can do that by adding the -cleanargument to your eclipse command line.

您可能必须清除 OSGI 和 eclipse 运行时使用的缓存数据。您可以通过将-clean参数添加到 eclipse 命令行来实现。

Here is the relevant information from the Plug-in Dev Guide:

以下是插件开发指南中的相关信息:

Cleans cached data used by the OSGi framework and Eclipse runtime. Try to run Eclipse once with this option if you observe startup errors after install, update, or using a shared configuration.

清除 OSGi 框架和 Eclipse 运行时使用的缓存数据。如果您在安装、更新或使用共享配置后发现启动错误,请尝试使用此选项运行一次 Eclipse。

See the Eclipse runtime optionsfor more information.

有关更多信息,请参阅 Eclipse运行时选项

You might also want to check into using update sites. I routinely use a local update site to install plugins while testing as it seems less error prone. The Eclipse FAQ has a quick article on how to create an update site.

您可能还想检查使用更新站点。我经常使用本地更新站点在测试时安装插件,因为它似乎不太容易出错。Eclipse FAQ 有一篇关于如何创建更新站点的快速文章。

回答by Mario Ortegón

Another possibility then is that the plugin you are installed is cached in the installation. You can start eclipse with the -clean option to remove all the cached plugins from the workspace

另一种可能性是您安装的插件缓存在安装中。您可以使用 -clean 选项启动 eclipse 以从工作区中删除所有缓存的插件

回答by lothar

Sounds to me the eclipse application where you try to install your plugin into might not have the org.eclipse.ui.console plugin installed that you need to have as a plugin dependency. Did you check that your plugin has the correct plugin dependencies and that it is installed without a problem?

在我看来,您尝试将插件安装到的 eclipse 应用程序可能没有安装 org.eclipse.ui.console 插件,而您需要将其作为插件依赖项安装。您是否检查过您的插件是否具有正确的插件依赖项并且安装没有问题?