Loading modules at runtime
Sander Mak
sander.mak at luminis.eu
Wed Jan 11 18:59:35 UTC 2017
Indeed, loading a new module at runtime through Layer which provides a service written to an interface of the host application works brilliantly for these types of use cases. There's an overload on ServiceLoader [1] which takes a layer and returns the plugin instance from the newly loaded module.
Sander
[1] http://download.java.net/java/jdk9/docs/api/java/util/ServiceLoader.html#load-java.lang.reflect.Layer-java.lang.Class-
On 11 Jan 2017, at 17:08, Michael Rasmussen <michael.rasmussen at zeroturnaround.com<mailto:michael.rasmussen at zeroturnaround.com>> wrote:
See the javadoc for java.lang.reflect.Layer
http://download.java.net/java/jdk9/docs/api/java/lang/reflect/Layer.html
There is a simple example of something similar there.
/Michael
On Jan 11, 2017 6:04 PM, "Piotr Chmielewski" <piotr.ch.dev at gmail.com> wrote:
For one of my projects, I decided to create "plugin" system to load class
at runtime from JAR files. Currently, I load each plugin using
URLClassLoader.
With arrival of JDK9, is there any plans for loading modules at runtime
(or is available already for testing in build snapshots)? For example
instead of:
String pluginPath = "CPU_Monitor.jar";
URL pluginUrl = new URL(pluginPath);
URL[] urls = {pluginUrl};
URLClassLoader urlClassLoader = new URLClassLoader(urls);
Class<?> pluginClass = urlClassLoader.findClass("com.
example.cpu.monitoring.Monitor");
Would be possible to use something like this:
Module pluginModule = ModuleManager.getModule("com.e
xample.cpu.Monitoring");
Class<?> pluginClass = pluginModule.getClass("com.exa
mple.cpu.monitoring.Monitor");
I guess that something like this could be implemented as library outside
JDK, but such API could be great addition to Jigsaw.
More information about the jigsaw-dev
mailing list