Ramos
Ramos

Reputation: 91

Why i cant access the classes of my Eclipse plugin at Runtime?

I’m developing an Eclipse plugin, com.simple.plugin, with the following structure: Plugin Structure

The problem is that during runtime I cant access the classes of my own plugin. For example if I do the following code inside the SampleHandler.java:

Class cls = Class.forName("com.simple.handlers.SampleHandler");
Object obj = cls.newInstance();

I get the error:

java.lang.ClassNotFoundException: com.simple.handlers.SampleHandler cannot be found by com.simple.plugin_1.0.0.qualifier*

My manifest runtime option for classpath have the root of the plug-in, so I dont know what is wrong!

Upvotes: 4

Views: 957

Answers (2)

greg-449
greg-449

Reputation: 111142

Your SampleHander class is in the com.simple.plugin.handlers package not the com.simple.handlers package. So the correct code is:

Class<?> cls = Class.forName("com.simple.plugin.handlers.SampleHandler");

You must always specify the full package name of the class you want.

Upvotes: 2

Marcinek
Marcinek

Reputation: 2117

Eclipse plugins runs each with an own class loader. Thus you won't be able to dynamically load any class from an other bundle.

For this kind of problem there is a Buddy system in Eclipse osgi. You have to change your parent project buddy policy in the manifest.mf file:

Eclipse-BuddyPolicy: Registered

To make the classes from an other plugin project be aviable to your parent project add this to your manifest.mf file.

Eclipse-RegisterBuddy: {NAME OF THE PARENT PLUGIN}

For example:

Eclipse-RegisterBuddy: de.myname.myplugin

Now you will be able to load your class from both plugins.

See also here:

https://wiki.eclipse.org/Context_Class_Loader_Enhancements

Upvotes: 1

Related Questions