Skip to content

[Native Image] -H:Preserve=path= fails the build when a preserved serializable lambda captures a class with a missing field type #15240

Backlog

Description

@kimeta

Describe the issue

With -H:Preserve=path=<jar>, the build fails with "The build process encountered an unexpected error" when a preserved class creates a serializable lambda that captures a Serializable object whose class declares a field of a type that is missing from the classpath (an optional dependency). Without -H:Preserve, the same build passes.

The build process encountered an unexpected error:

java.lang.NoClassDefFoundError: B
	at java.base/java.lang.Class.getDeclaredFields0(Native Method)
	at java.base/java.lang.Class.privateGetDeclaredFields(Class.java:2917)
	at java.base/java.lang.Class.getDeclaredField(Class.java:2380)
	at java.base/java.io.ObjectStreamClass.getDeclaredSUID(ObjectStreamClass.java:1579)
	at java.base/java.io.ObjectStreamClass.<init>(ObjectStreamClass.java:355)
	...
	at java.base/java.io.ObjectStreamClass.lookup(ObjectStreamClass.java:227)
	at com.oracle.svm.hosted.reflect.serialize.SerializationBuilder.registerIncludingAssociatedClasses(SerializationFeature.java:354)
	at com.oracle.svm.hosted.reflect.serialize.SerializationBuilder.registerIncludingAssociatedClasses(SerializationFeature.java:367)
	at com.oracle.svm.hosted.reflect.serialize.SerializationBuilder.registerIncludingAssociatedClasses(SerializationFeature.java:320)
	at com.oracle.svm.hosted.reflect.serialize.SerializationBuilder.registerIncludingAssociatedClasses(SerializationFeature.java:278)
	at com.oracle.svm.hosted.image.PreserveOptionsSupport.lambda$registerPreservedClasses$2(PreserveOptionsSupport.java:250)
	at com.oracle.graal.pointsto.api.HostVM.notifyClassReachabilityListener(HostVM.java:134)
	at com.oracle.svm.hosted.NativeImageGenerator.lambda$runPointsToAnalysis$1(NativeImageGenerator.java:978)
	at com.oracle.graal.pointsto.AbstractAnalysisEngine.runAnalysis(AbstractAnalysisEngine.java:188)
	...
Caused by: java.lang.ClassNotFoundException: B

PreserveOptionsSupport.registerPreservedClasses registers a class-reachability listener. When a lambda class whose capturing class is preserved becomes reachable and is Serializable, the listener calls serialization.registerIncludingAssociatedClasses(always, reachedClass). That method walks the lambda's captured-argument field types and calls ObjectStreamClass.lookup on each one. ObjectStreamClass reads serialVersionUID through Class.getDeclaredField, which resolves the types of all declared fields, so it throws NoClassDefFoundError when one of them is missing. Nothing on this path handles LinkageError. By contrast, registerPreservedClass and registerType in the same file catch it and skip the registration. Whether the field is transient makes no difference.

Steps to reproduce the issue

lib/A.java:

import java.io.Serializable;
import java.util.function.Supplier;

public class A implements Serializable {
    private transient B b;

    public Supplier<A> supplier() {
        return (Supplier<A> & Serializable) () -> this;
    }
}

lib/B.java:

public class B {
}

app/Main.java:

public class Main {
    public static void main(String[] args) {
        System.out.println("Hello");
    }
}
mkdir -p lib-classes app-classes
javac -d lib-classes lib/A.java lib/B.java
rm lib-classes/B.class   # B stands for an optional dependency that is not on the classpath
jar cf a.jar -C lib-classes A.class
javac -d app-classes app/Main.java
native-image -cp a.jar:app-classes -H:+UnlockExperimentalVMOptions -H:Preserve=path=$PWD/a.jar -o main Main

The build fails with java.lang.NoClassDefFoundError: B and the stack trace above. Main does not reference A. The same command without -H:+UnlockExperimentalVMOptions -H:Preserve=path=$PWD/a.jar builds, and ./main prints Hello. -H:+UnlockExperimentalVMOptions only silences the warning that -H:Preserve is experimental; without it, the result is the same.

Describe GraalVM and your environment

  • Fails:
    • GraalVM CE 25.6.5.1+2.1 (build 25.0.5.1+2-jvmci-25.6-b29)
    • Oracle GraalVM 25.6.0-dev+0.1 (build 25.0.5.1+0-LTS-jvmci-25.6-b27)
    • GraalVM CE 25.5.5.0.3+1.1 (build 25.0.5.0.3+1-jvmci-25.5-b26)
    • The listener and registerIncludingAssociatedClasses are unchanged on master as of 2026-11-30.
  • Passes: Oracle GraalVM 25.0.4+7.1 (build 25.0.4+7-LTS-jvmci-b01). Its registerPreservedClasses has no class-reachability listener for lambdas.
  • OS: Linux x86_64

More details

Expected: the builder should skip (or warn about) a class whose field types cannot be resolved rather than fail the build. This is how it already handles missing optional classes elsewhere, including in registerPreservedClass.

We hit this with com.hazelcast:hazelcast:5.5.0 built with -H:Preserve=path=<hazelcast jar>. com.hazelcast.jet.impl.connector.WriteJmsP$Supplier creates a serializable SupplierEx lambda that captures this, and it declares private transient jakarta.jms.Connection connection. jakarta.jms is an optional dependency, so the build fails with NoClassDefFoundError: jakarta/jms/Connection.

#14810 reports other LinkageErrors that preserve registration does not ignore, on the reflection path (ReflectionDataBuilder.linkType). This failure is on the serialization path and needs neither Crema nor --link-at-build-time.

The trace also contains Suppressed: VMError$HostedError: Reflection metadata cannot be queried before analysis is complete. That is #14823 and is unrelated to this failure.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions