26

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?

2
  • 2
    JPMS is not OSGi, as stated in section 2.5 of JSR 376: OSGi’s life-cycle and dynamic service-registry facilities are … beyond the scope of [JPMS] Sep 8, 2017 at 22:56
  • 1
    @Pavel_K Your comment confuses me. In my comment and link I show that the JPMS is explicitly and self-consciously not aiming to provide the dynamic lifecycle features found in OSGi, and that OSGi will continue to operate as always in Java 9. Yet you seem to talk right past me and past the Answers provided below. Exactly what about this issue are you not understanding? Sep 9, 2017 at 6:58

2 Answers 2

14

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.

0
6

The other answer is fully correct, but please note that "in the end" these things didn't really change.

Before Java 9 you could use custom class loader instances to achieve something like this. That is for example how application servers such as Tomcat allow you to re-deploy an application - by basically throwing away a whole "context" that was initially "built" using a specific class loader instance.

With Java9, this concept is described using that layers abstraction - but in the end it still means that custom code needs to provide all the implementation of actually creating layers with different class loaders.

And for some further read on layers see this answer that I gave some time back on a similar question (which focused on how to use different versions of the same module within a single application).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.