4

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!

David Yee
  • 3,515
  • 25
  • 45
Ramos
  • 91
  • 7
  • I can't reproduce this. You need to show us a more complete example. – greg-449 Jan 27 '16 at 19:17
  • What i did was simply create: a plug-in project-> use the template hello command. Then it generates the structures that i revealed. Then add the code line Class cls = Class.forName("PluginName.SampleHandler") to some class in the plugin. And i think you wil get the same error when trying to run. – Ramos Jan 27 '16 at 19:18

2 Answers2

2

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.

greg-449
  • 109,219
  • 232
  • 102
  • 145
1

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

Marcinek
  • 2,144
  • 1
  • 19
  • 25
  • There only appears to be one plugin involved here so this doesn't apply. – greg-449 Jan 27 '16 at 18:47
  • yes it is only a plugin involved (com.simple.plugin), and i was trying during runtime of that plugin call some of his classes. – Ramos Jan 27 '16 at 19:00