是否可以在Java 9中动态加载和卸载jdk和自定义模块? [英] Is it possible to load and unload jdk and custom modules dynamically in Java 9?
问题描述
我是JPMS的初学者,无法理解其动态性.例如,在当前的JVM实例中,moduleA.jar
正在运行. moduleA
仅需要java.base
模块.现在,我要
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
- 动态加载需要
java.sql
模块和moduleC.jar
的 - 从
moduleB
执行一些代码
- 从JVM卸载
moduleB
,java.sql
,moduleC
并释放所有资源.
moduleB.jar
- to load dynamically
moduleB.jar
that needsjava.sql
module andmoduleC.jar
- execute some code from
moduleB
- unload
moduleB
,java.sql
,moduleC
from JVM and release all resources.
可以在Java 9模块系统中完成吗?
Can it be done in Java 9 module system?
推荐答案
这是高级主题.从较高的层次上讲,模块系统不是动态的,因为单个模块不能在正在运行的VM中卸载或替换.但是,您可以使用API,特别是java.lang.module.Configuration
和java.lang.ModuleLayer
来创建模块的动态配置,并在运行的VM中将它们实例化为模块层.在您的情况下,您可以创建一个包含模块B和C的模块层.一旦没有对它们的引用,将对这层模块进行GC/卸载.
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.
这篇关于是否可以在Java 9中动态加载和卸载jdk和自定义模块?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!