Code Monkey home page Code Monkey logo

urlclassloader-loads-plugin's Introduction

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

urlclassloader-loads-plugin's People

Contributors

rae-zou avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.