在JRE-9/lib目录中(至少在Windows上),有一个新文件,modules其大小约为107 MB.是否可以提取该文件或列出其中的Java模块?
我可以看到一个名为的新工具jmod可用jdk-9/bin/jmod.exe,但是用于读取.jmod位于的文件,jdk-9/jmods它无法读取文件modules.
我想了解如何Threadlocal导致Classloader泄漏。所以为此我有这个代码
public class Main {
public static void main(String... args) throws Exception {
loadClass();
while (true) {
System.gc();
Thread.sleep(1000);
}
}
private static void loadClass() throws Exception {
URL url = Main.class.getProtectionDomain()
.getCodeSource()
.getLocation();
MyCustomClassLoader cl = new MyCustomClassLoader(url);
Class<?> clazz = cl.loadClass("com.test.Foo");
clazz.newInstance();
cl = null;
}
}
class MyCustomClassLoader extends URLClassLoader {
public MyCustomClassLoader(URL... urls) {
super(urls, null);
}
@Override
protected void finalize() {
System.out.println("*** CustomClassLoader finalized!");
}
}
Run Code Online (Sandbox Code Playgroud)
Foo.java
public class Foo { …Run Code Online (Sandbox Code Playgroud) 从 Java 11 开始,如何读取另一个运行时映像的内容?
为了列出 Java 运行时映像的内容,JEP 220建议了以下解决方案:
URL 方案的内置 NIO
FileSystem提供程序jrt确保开发工具可以通过加载FileSystemURL 命名的来枚举和读取运行时映像中的类和资源文件jrt:/,如下所示:Run Code Online (Sandbox Code Playgroud)FileSystem fs = FileSystems.getFileSystem(URI.create("jrt:/")); byte[] jlo = Files.readAllBytes(fs.getPath("modules", "java.base", "java/lang/Object.class"));
此代码段有效,并允许我读取java/lang/Object.class正在执行代码的 Java 安装的运行时映像中的内容。
java/lang/Object.class考虑到它的 java home,我怎样才能让它读取另一个 Java 安装中的内容?
我已经阅读了这个 SO answer,它解释了如何从 Java 8 运行时读取 Java 运行时图像的内容。不幸的是,这不适用于较新的 Java 运行时,因为我相信 for 的文件系统jrt:/将始终指向当前的运行时映像。