Description
In graalvm-jdk-21.0.5+9.1, a Java program encounters an unexpected java.lang.NoClassDefFoundError: Could not initialize class java.lang.StackTraceElement$HashedModules despite the class B1903 being successfully loaded at startup by the system class loader (AppClassLoader). The program uses a custom ClassLoader to load B1903 and invokes its main method via reflection, leading to limited recursion before failing with this error. Since B1903 is already loaded as the entry point class, the NoClassDefFoundError seems unjustified. In contrast, the same code on graalvm-jdk-17 runs as expected, entering full recursion and throwing StackOverflowError without this issue.
Steps to reproduce the issue
testcase
public class B1903 {
public static void main(String[] args) {
try {
ClassLoader customLoader = new ClassLoader() { };
Class<?> clazz = customLoader.loadClass("B1903");
clazz.getDeclaredMethod("main", String[].class).invoke(null, (Object) args);
} catch (Exception e) {
e.printStackTrace();
}
}
}
graalvm-jdk-21.0.5+9.1/bin/javac B1903.java
graalvm-jdk-21.0.5+9.1/bin/java B1903
output:
Exception in thread "main" java.lang.NoClassDefFoundError: Could not initialize class java.lang.StackTraceElement$HashedModules
Exception: java.lang.NoClassDefFoundError thrown from the UncaughtExceptionHandler in thread "main"
On graalvm-jdk-17, the same code recurses fully until a StackOverflowError, as expected. In graalvm-jdk-21.0.5+9.1, the recursion stops prematurely with NoClassDefFoundError, referencing StackTraceElement$HashedModules. Since B1903 is already loaded and accessible, this error is unexpected and suggests a failure in an internal JVM class during recursion, not a problem with B1903 itself. I believe this could indicate a latent issue in GraalVM JDK 21, possibly related to stricter runtime checks or a regression in handling recursive reflection calls.