After the theory behind plugin-based design, the way to load plugin jars at runtime becomes important. Thanks to the well-developed java.net.URLClassLoader Utilities, which can be used to access to the custom-plugins. "URL class loader is used to load classes and resources from a search path of URLs referring to both JAR files and directories." https://docs.oracle.com/javase/7/docs/api/java/net/URLClassLoader.html
By having an interface to specify some behaviors. Example PluginInterface is shown in pluginDemoModule/src/.
Having the custom plugin classes implement the interface, we could cast the custom plugin back to PluginInterface, which made them easy to work with. Example Plugin1 and Plugin2 are shown in pluginModule/src/. These custom plugin classes need to be built in a seperate module and exported as a jar.
To creat an independent module of the project in IntelliJ:
- File -> Project Structure -> Modules ->Add -> New Module -> Dependencies -> Add -> Module Dependency -> Choose
pluginDemoModule-> Apply
To produce the plugin jar files of the pluginModule in IntelliJ:
-
Build Project -> File -> Project Structure -> Artifacts -> Add -> Jar From modules dependencies-> Create JAR from modules -> Select
pluginModuleas Module and select the Directory for META-INF/MANIFEST.MF -> Apply -
Before compile and build the JAR file, one important step is to include the configuration files in
META-INF/servicesdirectory. -
Build project -> Build Artifacts -> Get the jar file from
out
- use
Privilegedblocks catch the illegal access.
Finally, you can load the plugin jar files using URLClassLoader, after that you will be able to access to the custom plugin classes. Example LoadPluginFile is shown in pluginDemoModule/src/.



