Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
fmeum opened this issue Feb 12, 2023 · 1 comment · Fixed by #1048
Closed

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

fmeum opened this issue Feb 12, 2023 · 1 comment · Fixed by #1048

Comments

@fmeum
Copy link

fmeum commented Feb 12, 2023

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.

@cushon
Copy link
Collaborator

cushon commented Feb 14, 2023

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!");
    }
  }
}

vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 9, 2024
This uses a container to simplify GraalVM installation.
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 10, 2024
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 10, 2024
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 10, 2024
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 11, 2024
vorburger added a commit to vorburger/google-java-format that referenced this issue Feb 11, 2024
cushon pushed a commit that referenced this issue Feb 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants