Skip to content

Add native image builds #358

Closed
Closed

Description

@saturnism

It would be awesome to produce a native binary build using graalvm's native-image as part of the release.

native-image seems to work well w/ the current all-deps jar.

native-image -jar google-java-format-1.7-all-deps.jar

Activity

  1. natros commented on Mar 28, 2019

    @natros

    wow! realy fast formting.

    in my case I had to pass -H:IncludeResourceBundles=org.openjdk.tools.javac.resources.compiler

  2. natros commented on Oct 30, 2019

    @natros

    graalvm 19.2.1

    #!/usr/bin/env bash
    
    die() {
      echo "${@}" 1>&2
      exit 1
    }
    
    version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
    
    jar="core/target/google-java-format-${version}-all-deps.jar"
    
    if [[ ! -f "${jar}" ]]; then
      echo "building ${jar}..."
      mvn clean verify -DskipTests -pl core -am || die "cannot build ${jar}"
    fi
    
    echo "building native image from ${jar}..."
    native-image \
        -H:+ReportExceptionStackTraces \
        -H:+ReportUnsupportedElementsAtRuntime \
        -H:+TraceClassInitialization \
        -H:IncludeResourceBundles=org.openjdk.tools.javac.resources.compiler \
        -H:Class=com.google.googlejavaformat.java.Main  \
        -H:Name="google-java-format" \
        -cp "${jar}" \
        --initialize-at-build-time\
        --no-fallback \
        --no-server
    echo "done"
  3. joprice commented on May 29, 2020

    @joprice

    This is great. Any reason it couldn't be the main distribution format?

  4. perezd commented on Sep 27, 2020

    @perezd
    Contributor

    Trying this with Graal CE Java11/20.2.0 + GJF 1.9 I get the following error:

    java.lang.IllegalStateException: Unknown Java version: 11
    	at com.google.googlejavaformat.java.Formatter.getMajor(Formatter.java:193)
    	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:157)
    	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:291)
    	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
    	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
    	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	at java.lang.Thread.run(Thread.java:834)
    	at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:517)
    	at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192)
    

    This was the command I used:

    native-image \
    -H:+ReportExceptionStackTraces \
    -H:+ReportUnsupportedElementsAtRuntime \
    -H:+TraceClassInitialization \
    -H:Class=com.google.googlejavaformat.java.Main \
    -H:IncludeResourceBundles=com.sun.tools.javac.resources.compiler \
    -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
    -H:Name="google-java-format" \
    -jar google-java-format-1.9-all-deps.jar \
    --initialize-at-build-time \
    --no-fallback \
    --no-server 
    
  5. natros commented on Sep 28, 2020

    @natros

    This is what I did (I'm using GraalVM Enterprise Edition 11-20.2.0 on macOS, should work with Comunity Edition)

    java --version
    java 11.0.8.0.2 2020-07-09 LTS
    Java(TM) SE Runtime Environment GraalVM EE 20.2.0 (build 11.0.8.0.2+1-LTS-jvmci-20.2-b03)
    Java HotSpot(TM) 64-Bit Server VM GraalVM EE 20.2.0 (build 11.0.8.0.2+1-LTS-jvmci-20.2-b03, mixed mode, sharing)

    First, I build the google-java-format-1.10-SNAPSHOT-all-deps.jar with java provided by graalvm.

    mvn clean verify -DskipTests -Dmaven.javadoc.skip=true -pl core -am 

    Then I selected a few projects and created a file all.txt with all java files:

    echo -i > all.txt
    find ${HOME}/Projects/jetty.project -type f -name \*.java >> all.txt
    

    and did the same with few more projects.

    Then inside google-java-format I did

    mkdir -p core/src/main/resources/META-INF/native-image
    java -agentlib:native-image-agent=config-output-dir=core/src/main/resources/META-INF/native-image -jar core/target/google-java-format-1.10-SNAPSHOT-all-deps.jar @all.txt
    

    This will create the json files with information for graalvm

    And finally

    native-image \
        -H:+ReportExceptionStackTraces \
        -H:+ReportUnsupportedElementsAtRuntime \
        -H:+TraceClassInitialization \
        -H:ConfigurationFileDirectories=/Users/fsousa/Projects/code/google-java-format/core/src/main/resources/META-INF/native-image \
        -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
        -H:Class=com.google.googlejavaformat.java.Main  \
        -H:Name="google-java-format" \
        -Dgraal.EnterprisePartialUnroll=true \
        -cp "${jar}" \
        --initialize-at-build-time\
        --no-fallback \
        --no-server
    

    Note: remove -Dgraal.EnterprisePartialUnroll=true for comunity edition

    $ time google-java-format --version
    google-java-format: Version 1.10-SNAPSHOT
    
    real	0m0.006s
    user	0m0.002s
    sys	0m0.002s
    

    I have a script that resumes what I did.

    #!/usr/bin/env bash
    #
    # echo -i > all.txt
    # find /Users/fsousa/Projects/ipb/gdoc2 -type f -name \*.java >> all.txt
    # find /Users/fsousa/Projects/ipb/candidaturas -type f -name \*.java >> all.txt
    # find /Users/fsousa/Projects/egoi/te-proxy -type f -name \*.java >> all.txt
    # find /Users/fsousa/Projects/code/jetty.project -type f -name \*.java >> all.txt
    # mkdir -p core/src/main/resources/META-INF/native-image
    # java -agentlib:native-image-agent=config-output-dir=core/src/main/resources/META-INF/native-image -jar core/target/google-java-format-1.10-SNAPSHOT-all-deps.jar @all.txt
    
    die() {
      echo "${@}" 1>&2
      exit 1
    }
    
    version=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
    
    jar="core/target/google-java-format-${version}-all-deps.jar"
    
    if [[ ! -f "${jar}" ]]; then
      echo "building ${jar}..."
      mvn clean verify -DskipTests -Dmaven.javadoc.skip=true -pl core -am || die "cannot build ${jar}"
    fi
    
    
    #-H:ConfigurationResourceRoots=META-INF/native-image \
    
    echo "building native image from ${jar}..."
    native-image \
        -H:+ReportExceptionStackTraces \
        -H:+ReportUnsupportedElementsAtRuntime \
        -H:+TraceClassInitialization \
        -H:ConfigurationFileDirectories=/Users/fsousa/Projects/code/google-java-format/core/src/main/resources/META-INF/native-image \
        -H:IncludeResourceBundles=com.sun.tools.javac.resources.javac \
        -H:Class=com.google.googlejavaformat.java.Main  \
        -H:Name="google-java-format" \
        -Dgraal.EnterprisePartialUnroll=true \
        -cp "${jar}" \
        --initialize-at-build-time\
        --no-fallback \
        --no-server
    echo "done"

    for the command line, I have 2 scripts:

    java-format

    #!/usr/bin/env bash
    
    TMPFILE=$(mktemp)
    echo "-i" > ${TMPFILE}
    find . -type f -name \*.java >> ${TMPFILE}
    google-java-format @${TMPFILE}

    and java-format-changes

    #!/usr/bin/env bash
    
    files=$(git diff --name-only|grep \.java$)
    [[ -z ${files} ]] || google-java-format -i ${files}

    I'm using File Watchers plugin for IntelliJ, which formats the current file when the file is saved or format all unsaved files when compiling. Anyway, formating a file is faster than formating with the google plugin, especially when dealing with big files.

    image

  6. perezd commented on Oct 6, 2020

    @perezd
    Contributor

    So I followed your and it compiled but I get this error:

    java.lang.IllegalStateException: Unknown Java version: 11
    	at com.google.googlejavaformat.java.Formatter.getMajor(Formatter.java:193)
    	at com.google.googlejavaformat.java.Formatter.format(Formatter.java:157)
    	at com.google.googlejavaformat.java.Formatter.getFormatReplacements(Formatter.java:291)
    	at com.google.googlejavaformat.java.Formatter.formatSource(Formatter.java:267)
    	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:45)
    	at com.google.googlejavaformat.java.FormatFileCallable.call(FormatFileCallable.java:26)
    	at java.util.concurrent.FutureTask.run(FutureTask.java:264)
    	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    	at java.lang.Thread.run(Thread.java:834)
    	at com.oracle.svm.core.thread.JavaThreads.threadStartRoutine(JavaThreads.java:517)
    	at com.oracle.svm.core.posix.thread.PosixJavaThreads.pthreadStartRoutine(PosixJavaThreads.java:192)
    

    This is with the released jar google-java-format-1.9-all-deps.jar Also tried verbatim your steps w/ a copy of master repo same error. I am building w/ Java 11 if that matters.

  7. added a commit that references this issue on Sep 13, 2024
    d52e308
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