简体   繁体   English

Eclipse插件:启动后立即运行代码

[英]Eclipse plugin: Run code right after startup

I want to show a message right after the plugin starts. 我想在插件启动后立即显示一条消息。 If I put my code at the end of the Activator.start() method I get an error (probably because the needed resources are not loaded yet). 如果我将我的代码放在Activator.start()方法的末尾,我会收到一个错误(可能是因为尚未加载所需的资源)。

The errors look like this: 错误看起来像这样:

!MESSAGE While loading class "de.stefansurkamp.package.ClassIWantToLoad", thread "Thread[main,6,main]" timed out waiting (5006ms) for thread "Thread[Worker-2,5,main]" to finish starting bundle "MyPlugin_1.0.0.qualifier [302]". To avoid deadlock, thread "Thread[main,6,main]" is proceeding but "de.stefansurkamp.package.ClassIWantToLoad" may not be fully initialized.
!STACK 0
org.osgi.framework.BundleException: State change in progress for bundle "reference:file:/C:/Users/Stefan/workspace/MyPlugin/" by thread "Worker-2".
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.beginStateChange(AbstractBundle.java:1088)
    at org.eclipse.osgi.framework.internal.core.AbstractBundle.start(AbstractBundle.java:298)
    at org.eclipse.osgi.framework.util.SecureAction.start(SecureAction.java:478)
    at org.eclipse.osgi.internal.loader.BundleLoader.setLazyTrigger(BundleLoader.java:263)
    at org.eclipse.core.runtime.internal.adaptor.EclipseLazyStarter.postFindLocalClass(EclipseLazyStarter.java:109)
    [...]
Caused by: org.eclipse.osgi.framework.internal.core.AbstractBundle$BundleStatusException
    ... 53 more
Root exception:
org.eclipse.osgi.framework.internal.core.AbstractBundle$BundleStatusException
    [...]

If needed, I can provide a full stack trace. 如果需要,我可以提供完整的堆栈跟踪。

So is there any method that runs right after start() is finished? 那么有没有任何方法在start()完成后start()运行?

EDIT: Using a job and org.eclipse.ui.startup 编辑:使用作业和org.eclipse.ui.startup

According to greg's recommendations I created a Job inside a class which is loaded by the org.eclipse.ui.startup extension point. 根据greg的建议,我在一个由org.eclipse.ui.startup扩展点加载的类中创建了一个Job。

This is how my earlyStartup() method looks like: 这就是我的earlyStartup()方法的样子:

@Override
public void earlyStartup() {        
    Job clearPrefsJob = new Job("clearPrefsJob") {

        @Override
        protected IStatus run(IProgressMonitor monitor) {
            try {
                DataStorage dataStorage = new DataStorage();
                dataStorage.clearPrefs();
                System.out.println("Everything gone.");
            }
            catch (StorageException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

    };

    clearPrefsJob.addJobChangeListener(new IJobChangeListener() {
        (All of the IchangeEvents here)
    });

    clearPrefsJob.setPriority(Job.BUILD);
    clearPrefsJob.schedule();

My console prints Everything gone. 我的控制台打印Everything gone. and my debugging message for the job being done (by the job change listener). 和我正在完成的工作的调试消息(由作业更改侦听器)。

But right after that I get an error message: 但就在那之后我收到一条错误消息:

!ENTRY org.eclipse.core.jobs 4 2 2014-07-29 11:53:12.481
!MESSAGE An internal error occurred during: "clearPrefsJob".
!STACK 0
java.lang.NullPointerException
    at org.eclipse.core.internal.jobs.Worker.run(Worker.java:69)

Additionally my testing environment shows the following error message: 此外,我的测试环境显示以下错误消息:

'clearPrefsJob' has encountered a problem.
   An internal error occured during: "clearPrefsJob".
   java.lang.NullPointerException

I have no idea where the NullPointerException in this part of the code should come from because everything worked fine before using a job. 我不知道代码的这一部分中的NullPointerException应该来自哪里,因为在使用作业之前一切正常。

Solution: 解:

Looks like the problem was using return null . 看起来问题是使用return null When using return Status.OK_STATUS (or similar) everything went fine. 当使用return Status.OK_STATUS (或类似)时,一切都很顺利。

You could create a Job ( org.eclipse.core.runtime.jobs.Job ) in the start method and schedule it to run. 您可以在start方法中创建一个Joborg.eclipse.core.runtime.jobs.Job )并安排它运行。

If your code needs to interact with the UI use UIJob . 如果您的代码需要与UI交互,请使用UIJob

Alternatively you could use the org.eclipse.ui.startup extension point to have a class run early in Eclipse startup (but this may be too early for what you want to do so a Job may still be necessary). 或者,您可以使用org.eclipse.ui.startup扩展点在Eclipse启动时尽早运行类(但这对于您想要执行的操作可能为时尚早,因此可能仍需要Job)。

You could also use an OSGi service for your class. 您也可以为您的班级使用OSGi服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM