Java – Eclipse Plug-in: run the code immediately after startup
I want to display a message immediately after the plug-in starts If I put my code in activator At the end of the start () method, I receive an error (probably because the required resources have not been loaded yet)
The error looks 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,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 necessary, I can provide a complete stack trace
So is there any method that runs immediately after start() completes?
Edit: using assignments and org eclipse. ui. startup
According to Greg's suggestion, I'm in an organization by org eclipse. ui. A job is created in the class loaded by the startup extension point
This is what 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 print is all gone And debug messages for the work I'm doing (job change listener)
But just after that, I received 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)
In addition, my test environment displays the following error message:
'clearPrefsJob' has encountered a problem. An internal error occured during: "clearPrefsJob". java.lang.NullPointerException
I don't know where the NullPointerException in this part of the code should come from, because everything is normal before using the job
Solution:
It seems that the problem is to use return null When used, returns status OK_ Status (or similar), everything is going well
Solution
You can 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 can use org eclipse. ui. The startup extension point runs classes as early as eclipse starts (but this may be too early for what you want to do, so you may still need a job)
You can also use OSGi services for your class