Is it possible to force someone else's java ClassLoader to load an external byte array containing the bytecode of a valid Java class?

大兔子大兔子 提交于 2020-01-23 03:12:05

问题


I'm trying to force the System java classloader (i.e. ClassLoader.getSystemClassLoader()) to load an external class defined by a byte array with valid bytecode so that other classes subsequently loaded by this classloader can know about and instantiate the external class without getting a NoClassDefFoundError.

This surely does not work as it only defines the class on the classloader created, not in the System classloader:

URLClassLoader child = 
   new URLClassLoader(new URL[] { myJar.toURI().toURL()                          
   , ClassLoader.getSystemClassLoader());
Class.forName ("com.MyClass", true, child);

The code above will define com.MyClass for the child classloader, not for the system classloader.

Any way of accomplishing that?


回答1:


You can use Reflection with access override:

Method define = ClassLoader.class.getDeclaredMethod("defineClass",
                                      String.class, byte[].class, int.class, int.class);
define.setAccessible(true);
Class<?> clazz = (Class<?>)define.invoke(ClassLoader.getSystemClassLoader(),
                                      null, array, 0, array.length);

The access override is needed because we’re invoking a protected method, but being a protected method, it’s still part of the API, which exists in all implementations and won’t go away in future versions.


Java 9 introduced an astonishing simple way to achieve the same without a hack, as long as your own class has been loaded through the application class loader as well (as is the default):

Class<?> clazz = MethodHandles.lookup().defineClass(array);

This simply creates the class within the same class loading context as the class containing this statement.



来源:https://stackoverflow.com/questions/48691773/is-it-possible-to-force-someone-elses-java-classloader-to-load-an-external-byte

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