No such field exception while loading “scl” field from Classloader

时光怂恿深爱的人放手 提交于 2019-12-11 19:13:28

问题


I am moving my code from JDK 8 to Open JDK 12. While doing so, I am facing following issue :

java.lang.NoSuchFieldException: scl

while trying to call ClassLoader.class.getDeclaredField("scl"). This was working fine in Java 8 but no longer works in newer Java versions.

I did some findings and believe it has got to do with the way working of reflection and use of internal Java packages have changed since Java 8.

Set<URL> classLoaderUrls = computeClassLoaderUrls();
            ClassLoader bootstrapClassLoader = ClassLoader.getSystemClassLoader().getParent();
            this.classLoader = new URLClassLoader(classLoaderUrls.toArray(new URL[classLoaderUrls.size()]), bootstrapClassLoader);


            Field systemClassLoaderField = ClassLoader.class.getDeclaredField("scl");
            systemClassLoaderField.setAccessible(true);
            this.initialSystemClassLoader = (ClassLoader) systemClassLoaderField.get(null);
            systemClassLoaderField.set(null, this.classLoader);

I want to know , if the way to access these classloader and its fields has changed or should I use some other field instead of scl. Thanks

来源:https://stackoverflow.com/questions/57370434/no-such-field-exception-while-loading-scl-field-from-classloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!