想改进这个问题吗? 通过 editing this post 添加细节并澄清问题.
关闭 1 年前。
ClassLoader
clMain -> URLClassLoader
clA(A 类,$B)。
$B 是类 A 的内部类 B。
ClassLoader
加载,通过接口(interface)执行对象 A 的方法没有问题。NoClassDefFoundError
。this.getClass().getClassLoader().loadClass("A$B");
因此,提示是:“为什么在步骤 3 中找不到类 $B 的定义,可以用另一种方式解决这个问题吗?”
检查方法(this.getClass().getClassLoader()
)中使用的ClassLoader
,并将其与方法中使用的ClassLoader
进行比较构造函数 - 相同并包含必要的 url。
Java 8 和 14 尝试运行,jdk 8 构建。
编辑:
public class A {
public A(){
this.getClass().getClassLoader().loadClass("A$B"); // success
}
public void foo(){
this.getClass().getClassLoader().loadClass("A$B"); // success
new B(); // success
}
public static final class B {}
}
public class A {
public A(){
}
public void foo(){
this.getClass().getClassLoader().loadClass("A$B"); // throw
new B(); // Edit: added this line to demostrate what is not the main diff between two examples
}
public static final class B {}
}
编辑:此行为与内部类无关,而是与 jar 中的每个类相关。简而言之,我需要迭代 jar 中的每个类并在使用它们之前手动加载它们,否则 jvm 将抛出 NoClassDefFoundError
。
相关文章:
python - 在 Python@3.10 上安装 cytoolz 时出现问题
javascript - 如何将 HTMLElement 转换为 JSX.Element
swiftui - 如何在 iOS 15 中使用 SwiftUI 在特定 View 中隐藏 TabB
sql - 传递大的逗号分隔值时,使用 Custom TABLE TYPE 作为参数而不是 SQL
javascript - 按顺序播放多个音轨,而不是同时播放
reactjs - 为什么我的 CSRF token 与 Laravel 和 Sanctum 不匹配
debugging - A/libc : Fatal signal 11 (SIGSEGV), 代码