F
F
frankwolf2020-11-04 19:11:58
Java
frankwolf, 2020-11-04 19:11:58

How to create a simple pluggable Java + Maven application?

I decided to make my Java desktop application expandable with plugins. The essence of loading plugins is as follows: the application searches in a certain directory for the jar file of the plugin and its descriptor describing the name, version, main class, etc., using the URLClassLoader, this plugin is loaded into the application. At the same time, the plugin and the application must have an API connected, which is used for interaction between the plugin and the application.

I implemented it and started testing. At the same time, the application, the plugin to it, the API use the Maven

API to connect to the application and the plugin in this way:

<dependency>
    <groupId>название</groupId>
    <artifactId>название api</artifactId>
    <version>версия api</version>
    <scope>system</scope>
    <systemPath>${basedir}/api.jar</systemPath>
  </dependency>


In the application, I load the plugin 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_api/PluginContext
Caused by: java.lang.ClassNotFoundException: name.package_api.PluginContext
I understand that the application does not see the API classes that are used inside the plugin. And how to be in this situation, I do not know.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
frankwolf, 2020-11-16
@frankwolf

Solution: Don't use API connection in this way

<dependency>
    <groupId>название</groupId>
    <artifactId>название api</artifactId>
    <version>версия api</version>
    <scope>system</scope>
    <systemPath>${basedir}/api.jar</systemPath>
  </dependency>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question