Skip to content

Can't build native image with GraalVM 22.3.0 Java 17 CE #894

Closed

Description

@fmeum

A native image built via mvn -Pnative -DskipTests package with GraalVM 22.3.0 Java 17 CE fails at runtime with:

MyClass.java: error: com.sun.tools.javac.parser.Tokens$TokenKind
java.lang.NoClassDefFoundError: com.sun.tools.javac.parser.Tokens$TokenKind
	at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:350)
	at com.google.googlejavaformat.java.JavaInput.buildToks(JavaInput.java:335)
	at com.google.googlejavaformat.java.JavaInput.<init>(JavaInput.java:277)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:260)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:247)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.base@17.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base@17.0.5/java.lang.Thread.run(Thread.java:833)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)

While this seems to be fixed by adding -J--add-exports=... buildArgs to the native-maven-plugin config, the produced native image still fails at runtime with a different error:

MyClass.java: error: com.google.googlejavaformat.java.java17.Java17InputAstVisitor. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
java.lang.LinkageError: com.google.googlejavaformat.java.java17.Java17InputAstVisitor. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:162)
	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:271)
	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:247)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
	at java.base@17.0.5/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base@17.0.5/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base@17.0.5/java.lang.Thread.run(Thread.java:833)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.thread.PlatformThreads.threadStartRoutine(PlatformThreads.java:775)
	at org.graalvm.nativeimage.builder/com.oracle.svm.core.posix.thread.PosixPlatformThreads.pthreadStartRoutine(PosixPlatformThreads.java:203)
Caused by: java.lang.ClassNotFoundException: com.google.googlejavaformat.java.java17.Java17InputAstVisitor. This exception was synthesized during native image building from a call to java.lang.Class.forName(String) with constant arguments.
	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:157)
	... 10 more

I haven't found a fix for this yet.

Activity

  1. cushon commented on Feb 14, 2023

    @cushon
    Collaborator

    Thanks for the report, we should fix the -Pnative configuration.

    This invocation works for me, I adapted it from the config we're using internally. I haven't verified if all of these flags are still necessary:

    ./graalvm-ce-java17-22.3.1/bin/native-image \
      --verbose \
      -J--add-modules \
      -JALL-SYSTEM \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.file=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.parser=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED \
      -J--add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED \
      --no-fallback \
      --initialize-at-build-time=com.sun.tools.javac.file.Locations \
      -H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler \
      -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
      -jar google-java-format-HEAD-SNAPSHOT-all-deps.jar
    
    $ cat ~/src/google-java-format/core/src/test/resources/com/google/googlejavaformat/java/testdata/I684.input | ./google-java-format-HEAD-SNAPSHOT-all-deps -
    package example;
    
    import example.model.SealedInterface;
    import example.model.TypeA;
    import example.model.TypeB;
    
    public class Main {
      public void apply(SealedInterface sealedInterface) {
        switch (sealedInterface) {
          case TypeA a -> System.out.println("A!");
          case TypeB b -> System.out.println("B!");
        }
      }
    }
    
  2. added 13 commits that reference this issue on Feb 9, 2024
    f4a79ea
    0373cd8
    e7732aa
    ad87d2b
    b8908dc
    31d5cb0
    081f57b
    3ad4094
    29434cd
    de50736
    acf07d0
    3c749bd
    852553f
  3. added a commit that references this issue on Feb 11, 2024
    3d9c8fa
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