Skip to content

Rae-Zou/URLClassLoader-loads-plugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

32 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Use URLClassLoader loads plugin jars at runtime

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

Step 1: creat the plugin interface

By having an interface to specify some behaviors. Example PluginInterface is shown in pluginDemoModule/src/.

Step 2: creat your custom plugins which implement the plugin interface

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

image

Step 3: produce the plugin jar file

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 pluginModule as Module and select the Directory for META-INF/MANIFEST.MF -> Apply

    image

  • Before compile and build the JAR file, one important step is to include the configuration files in META-INF/services directory.

    image

  • Build project -> Build Artifacts -> Get the jar file from out

Step 4: load the plugin jar file

  • use Privileged blocks 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/.

image

About

Use URLClassLoader loads plugin jars at runtime.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages