C
C
cyberorg2013-02-14 12:54:42
Java
cyberorg, 2013-02-14 12:54:42

Dynamic loading and unloading of plugins at runtime (Java)?

What is now:
1) A Java program that uses Jetty Embedded and hangs on a specific port
2) Plugins for this program: a set of Java classes in packages (they are distributed in jar files without libraries)
Idea:
The command to enable is sent to the main servlet ( disable) plugin. Executes the desired method: when enabled, all classes of the plugin jar file become available, when disabled, the situation is reversed.
What did you manage to google?
About the classpath: that when enabled, you can expand the classpath to classes in the Jar of the plugin.
About dynamic class loading: using your own class loader.
Questions:
Is the idea implementable by simply modifying the classpath or not?
If so, what would the unloading look like (i.e. removing paths from the classpath)?
If not, are there any peculiarities in the implementation of Class Loader from Jar files.
How to deal with unloading (everywhere it is written that the Garbage Collector “unloads” classes (but, as far as I remember, the GC cannot be called by itself, it is called by the JVM when necessary).
Then another question: how to make the situation “when necessary” for classes that in the plugin jar
Thanks in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
javax, 2013-02-14
@javax

Inherit URLClassLoader, it remains to say where to get the classes from. When loading a plugin, create a new classloader and add it to your list of available classloaders; when unloading it, remove it from the list.
When you need to do something, you look for (I don’t know what exactly you need - by the name of the plugin? class?) The necessary classloader in your list and create an instance of the desired class from there.
Keep a list of classloaders somewhere - ServletContext or something like that
There are quite a few articles on the network on this topic

S
Sergey Sidorov, 2013-03-31
@ISergius

In order to flexibly manage plugins, it is better to use OSGi, as already advised here.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question