Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to load and unload jdk and custom modules dynamically in Java 9?

Tags:

java

java-9

I am beginner in JPMS and can't understand its dynamism. For example, in current JVM instance moduleA.jar is running. moduleA requires only java.base module. Now, I want

  1. to load dynamically moduleB.jar that needs java.sql module and moduleC.jar
  2. execute some code from moduleB
  3. unload moduleB, java.sql, moduleC from JVM and release all resources.

Can it be done in Java 9 module system?


1 Answers

This is an advanced topic. At a high-level, the module system is not dynamic in the sense that individual modules can not be unloaded or replaced in a running VM. However, you can use the API, specifically java.lang.module.Configuration and java.lang.ModuleLayer to create dynamic configurations of modules and instantiate them in a running VM as a layer of modules. In your scenario, then you may create a layer of modules with modules B and C. This layer of modules will be GC'ed/unloaded once there are no references to them.

As I said, this is an advanced topic and it's probably better to spend time mastering the basics, including services, before getting into dynamic configurations and module layers.

like image 182
Alan Bateman Avatar answered Sep 10 '25 08:09

Alan Bateman