Question

I am having problems using javassist when deploying an application as an Eclipse plugin, Concretely, the following code:

ClassPool.getDefault().get(aClassName);

is throwing a: javassist.NotFoundException.

The code works fine if not used as part of the plugin. I am trying to understand if the problem is in the configuration of the classpath of my plugin (it does not seem to be the case), or something related to the classloader Eclipse uses when executing a plugin project as an Eclipse Application.

The code that triggers the problem is part of a custom Eclipse editor. So it appears when the user try to open a document associated with the editor. The class javassist is attempting to load is part of the plugin classes.

Thanks in advance for any clue!.

Was it helpful?

Solution

From the documentation of ClassPool:

Returns the default class pool. The returned object is always identical since this method is a singleton factory.

The default class pool searches the system search path, which usually includes the platform library, extension libraries, and the search path specified by the -classpath option or the CLASSPATH environment variable.

The ClassPool returned by ClassPool.getDefault() only adds the system classpath. Eclipse plugins are not loaded by the system classloader so it will not be able to see your plugin's classes.

You need to add your plugin's classloader to the class pool so it can see those classes. This can be done through:

ClassLoader pluginLoader = ...;
ClassPool pool = ClassPool.getDefault();
pool.appendClassPath(new LoaderClassPath(pluginLoader));

where pluginLoader is the classloader of your plugin. The easiest way to get this is probably through

pluginLoader = <one of your plugin's classes>.class.getClassLoader();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top