因此,我有一个类加载器 ReflectionClassLoader,我用它从各个位置动态加载 JAR 文件,以便处理混淆的代码环境。
这在我的 IDE 中运行良好,并且玩起来非常有趣,但是当我在 Eclipse 中导出设置时,我发现我的类加载器正在由标准 URLClassLoader
加载,并且sun.misc.Launcher$AppClassLoader
,因此加载了两次。由于它存储某些数据,例如资源、加载的类等,因此它完全破坏了我的系统。在 Eclipse 中,它们都由 sun.misc.Launcher$AppClassLoader
加载。
第二次加载似乎在加载我的自定义 URL 协议(protocol) (debugrsrc) 的位置很明显。我尝试获取系统类加载器并进行反射,但这会返回 sun.misc.Launcher$AppClassLoader
,即使它没有在我的 main 方法中返回该值。
它似乎与程序的其余部分完全隔离。至于逃避,我正在考虑引入一些 Unsafe,尽管我更希望 Sun 不要搞乱 ClassLoaders。
最佳答案
我通过以下方式解决了这个问题:
ClassLoader oscl = sun.misc.Launcher.getLauncher().getClassLoader();
Field scl = ClassLoader.class.getDeclaredField("scl");
scl.setAccessible(true);
scl.set(null, oscl);
//reflect my real main method here, dont actually call the code because the system classloader will preload it.
关于Java sun.misc.Launcher$AppClassLoader 破坏代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30019937/