I developed an application that loads plugins dinamicaly according to this tutorial: http://solitarygeek.com/java/a-simple-pluggable-java-application, but i came across a problem. In my main application i have a class with static methods, how can i access that class from inside my plugins? When i try to access the class from the loaded plugin it gives me an error that class was not found, although the plugin and the application are running. Thank you
3 Answers
In the plugin architecture you are not supposed to access the plugin implementation class directly. What you have at your disposal is the interface through which you'll get access to an instance of your implementation class. That obviously precludes any static methods. Refactor those methods into instance methods and expose them via the interface.

- 195,646
- 29
- 319
- 436
-
Thank you for your response, but it seems that there is a way to do it – And Cost Jun 28 '12 at 11:14
-
There may be a way to do it if you tamper with the plugin infrastructure implementation, but it will just break that implementation. It is the fundamental requirement of a plugin architecture that the plugin should be seen from other plugins only through its public interface. There must be no static dependencies to another plugin's implementation classes as the other plugin has no right to assume they will be there. – Marko Topolnik Jun 28 '12 at 11:16
Sounds like a class loader problem. The class loader that loads your plugin does not see your main class. Difficult to say any more without code samples.

- 12,204
- 2
- 26
- 36
-
Thank you, i think you are right, i switched from sysLoader = (URLClassLoader) ClassLoader.getSystemClassLoader(); to sysLoader = (URLClassLoader) MainClass.class.getClassLoader(); and it seems to work – And Cost Jun 28 '12 at 11:12
I managed to resolve my problem by creting a classloader with the application classloader as parent classloader like this:
URLClassLoader MyLoader = new URLClassLoader(new URL[]{},MyClass.class.getClassLoader()), and now, all the classes loaded in my application are visible to the plugin that is loaded with MyLoader

- 95
- 8