[GR-74878] [Native Image] Defer Class.forName errors when building shared layers
Describe the Issue
Running a layered Quarkus app throws the following error:
$ LD_LIBRARY_PATH=target target/getting-started-1.0.0-SNAPSHOT-runner
Exception in thread "main" java.lang.RuntimeException: Should never be hit
at io.quarkus.runtime.Quarkus.launchFromIDE(Quarkus.java:14)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:94)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:50)
at io.quarkus.runtime.Quarkus.run(Quarkus.java:143)
at io.quarkus.runner.GeneratedMain.main(Unknown Source)Quarkus.run() calls Class.forName(Application.APP_CLASS_NAME, false, ...) where APP_CLASS_NAME = "io.quarkus.runner.ApplicationImpl" is a compile-time constant:
package io.quarkus.runtime;
public abstract class Application implements Closeable {
public static final String APP_CLASS_NAME = "io.quarkus.runner.ApplicationImpl";
...When building the base layer, ReflectionPlugins.processClassForName() intercepts Class.forName calls with constant arguments and tries to resolve the class via ImageClassLoader.findClass(). Since ApplicationImpl is an app-layer-generaed class that doesn't exist on the base layer classpath, GraalVM synthesizes a ClassNotFoundException that gets permanently baked into the compiled Quarkus.run() method.
At runtime the synthesized exception is caught by the catch (ClassNotFoundException e) block (which prints the stack trace), then execution falls through to launchFromIDE() - the dev-mode path - which throws RuntimeException: Should never be hit:
public class Quarkus {
public static void run(Class<? extends QuarkusApplication> quarkusApplication, BiConsumer<Integer, Throwable> exitHandler,
String... args) {
try {
//we already have an application, run it directly
Class<? extends Application> appClass = (Class<? extends Application>) Class.forName(Application.APP_CLASS_NAME,
false, Thread.currentThread().getContextClassLoader());
...
return;
} catch (ClassNotFoundException e) {
//ignore, this happens when running in dev mode
}
//dev mode path, i.e. launching from the IDE
//this is not the quarkus:dev path as it will augment before
//calling this method
launchFromIDE(quarkusApplication, args);
...One way to fix this would be on the GraalVM side, such that ReflectionPlugins.processClassForName does not throw an exception but returns false if building a shared layer:
if (!typeResult.isPresent()) {
if (RuntimeClassLoading.isSupported()) {
return false;
}
if (ImageLayerBuildingSupport.buildingSharedLayer()) {
/* The class might be present in an extension layer. */
return false;
}
Throwable e = typeResult.getException();
return throwException(b, targetMethod, null, arguments, e.getClass(), e.getMessage(), true);
}Using the latest version of GraalVM can resolve many issues.
- I tried with the latest version of GraalVM.
GraalVM Version
Master commit 87d8878ba50a69373963de12cb53a15454f1e971
Operating System and Version
Irrelevant
Troubleshooting Confirmation
- I tried the suggestions in the troubleshooting guide.
Run Command
NA
Expected Behavior
NA
Actual Behavior
NA
Steps to Reproduce
NA
Additional Context
NA
Run-Time Log Output and Error Messages
NA
Source: oracle/graal