是否可以在Java 9中动态加载和卸载jdk和自定义模块? [英] Is it possible to load and unload jdk and custom modules dynamically in Java 9?

查看:107
本文介绍了是否可以在Java 9中动态加载和卸载jdk和自定义模块?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是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

  1. 动态加载需要java.sql模块和moduleC.jar
  2. moduleB.jar
  3. moduleB
  4. 执行一些代码
  5. 从JVM卸载moduleBjava.sqlmoduleC并释放所有资源.
  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.

可以在Java 9模块系统中完成吗?

Can it be done in Java 9 module system?

推荐答案

这是高级主题.从较高的层次上讲,模块系统不是动态的,因为单个模块不能在正在运行的VM中卸载或替换.但是,您可以使用API​​,特别是java.lang.module.Configurationjava.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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆