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?
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.