- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
使用代码:
ModuleFinder.of(Paths.get(path)).findAll()
我能够检索到 Set<ModuleReference>
path
中的所有.jars文件夹。
我的下一步是获得 Module
来自 ModuleReference
但是没有返回那个的方法,我可以得到一个 ModuleDescriptor
但即使是那个也无济于事。有办法做到这一点吗?
最佳答案
如果您想访问模块内容,您应该open
您获得的 ModuleReference
。这将使您可以访问 ModuleReader
哪个
is intended for cases where access to the resources in a module is required
A resource in a module is identified by an abstract name that is a
'/'
-separated path string. For example, modulejava.base
may have a resource"java/lang/Object.class"
that, by convention, is the class file forjava.lang.Object
. A module reader may treat directories in the module content as resources (whether it does or not is module reader specific). Where the module content contains a directory that can be located as a resource then its name ends with a slash ('/'
). The directory can also be located with a name that drops the trailing slash.
请记住,文档还指定:
A ModuleReader is open upon creation and is closed by invoking the
close
method. Failure to close a module reader may result in a resource leak. Thetry-with-resources
statement provides a useful construct to ensure that module readers are closed.
从资源中获取 Module
的一种方法是使用 Class#getModule
访问它。作为:
Module module = com.foo.bar.YourClass.class.getModule();
编辑:随着时间的推移,我学会了使用 ModuleFinder
访问 Module
的更好方法正如 @Alan 所建议的那样也可能是:
ModuleFinder finder = ModuleFinder.of(path);
ModuleLayer parent = ModuleLayer.boot();
Configuration configuration = parent.configuration().resolve(finder, ModuleFinder.of(), Set.of("curious")); // 'curious' being the name of the module
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
ModuleLayer layer = parent.defineModulesWithOneLoader(configuration, systemClassLoader);
Module m = layer.findModule("curious").orElse(null);
关于java - 如何从 ModuleReference 获取 Module 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46180116/
这个问题在这里已经有了答案: Scanning classpath/modulepath in runtime in Java 9 (2 个答案) 关闭 4 年前。 更新:我在这里回答了我自己的问题
使用代码: ModuleFinder.of(Paths.get(path)).findAll() 我能够检索到 Set path 中的所有.jars文件夹。 我的下一步是获得 Module来自 Mod
我在 JPMS jdk.internal.loader 方面遇到了障碍。 BuiltinClassLoader在 IDE 上下文中执行主要方法时(我使用 IntelliJ 2018.1)。 如果使用标
我是一名优秀的程序员,十分优秀!