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.