0

I decided to make my desktop application in Java possible to extend it with plugins. The process of loading plugins is as follows: the application searches in a specific directory for the plugin jar file and its descriptor describing the name, version, main class, etc., using URLClassLoader this plugin is loaded into the application. In this case, the plugin and the application must have an API connected, which serves for the interaction between the plugin and the application.

I implemented this and started testing. In this case, the application, plug-in to it, API use Maven

The API connects to the application and plugin in this way:

<dependency>
<groupId>name</groupId>
<artifactId>api name</artifactId>
<version>api version</version>
<scope>system</scope>
<systemPath>${basedir}/api.jar</systemPath>

I load the plugin into the application like this:

loader = new URLClassLoader(new URL[] {info.getPluginJar().toURI().toURL()}, ClassLoader.getSystemClassLoader()); 
plugin = (Plugin) loader.loadClass(info.getMainClass()).newInstance();

But on startup, an error occurs:

Exception in thread "main" java.lang.NoClassDefFoundError: name/package/PluginContext
Caused by: java.lang.ClassNotFoundException: name.package.PluginContext

I understand that the application does not see the API classes that are used inside the plugin. And what to do in this situation, I do not know.

  • Does this answer your question? [What is a classpath and how do I set it?](https://stackoverflow.com/questions/2396493/what-is-a-classpath-and-how-do-i-set-it) – OrangeDog Nov 04 '20 at 16:24
  • Your code looks very similar to https://stackoverflow.com/questions/60764/how-to-load-jar-files-dynamically-at-runtime but I think you will need to load your class from the child classloader, not the system classloader as you can't change that one after startup. These child classloaders are used for things like that (I think) – wemu Nov 04 '20 at 18:29

0 Answers0