Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

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?

EDIT: Using a job and 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.

This is how my earlyStartup() method looks like:

@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. 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.

Solution:

Looks like the problem was using return null. When using return Status.OK_STATUS (or similar) everything went fine.

like image 504
Stefan Surkamp Avatar asked Feb 12 '23 09:02

Stefan Surkamp


1 Answers

You could create a Job (org.eclipse.core.runtime.jobs.Job) in the start method and schedule it to run.

If your code needs to interact with the UI use 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).

You could also use an OSGi service for your class.

like image 137
greg-449 Avatar answered Feb 15 '23 10:02

greg-449