Stay organized with collections
Save and categorize content based on your preferences.
Using the Cloud Client Libraries for Java, you can compile applications as
native images. This approach provides performance
benefits to your application by leveraging ahead-of-time compilation to
reduce the cold start-up time and memory footprint of your application.
Client library versions
Make sure that you are using version 25.4.0 or later of the
Cloud Libraries Bill of Materials (libraries-bom) or that you
are using client libraries published after May 2022.
Most users manage their client library versions using the libraries-bom.
This is an easy way to check that the versions of all the client libraries in
your project are compatible with each other and up-to-date.
Install the
Google Cloud CLI,
which allows you to run the sample with your project's credentials.
After installing the Google Cloud CLI,
log in with Application Default Credentials using the following command:
gcloud auth application-default login
Install an appropriate JDK distribution such as
GraalVM.
Make sure that you installed the native image extension by running the
following command:
gu install native-image
Verify that the default version of Java is set correctly by running
java -version in a terminal.
The output is similar to the following:
java -version
openjdk version "17.0.9" 2023-10-17
OpenJDK Runtime Environment GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22)
OpenJDK 64-Bit Server VM GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22, mixed mode, sharing)
Build an application as a native image
The following instructions use the native build tools plugins to build an
application as a native image.
Maven setup instructions
Copy and paste this text into your application's POM to use the native
Maven plugin
to build the native image for the application.
Be sure to replace MAIN_CLASS with the name of your main
class and VERSION with the version of the native Maven
plugin, such as 0.10.1.
Call ./gradlew nativeCompile to build the native image into the
./build/native/nativeCompile directory.
Run the application as an executable. For example,
./build/native/nativeCompile/$PROJECT_NAME.
With the plugin, the image name defaults to the project name.
Common Issues
SLF4J compatibility
If you are performing native image compilation on a client library with the SLF4J dependency, you may see the following built-time error.
Fatal error: com.oracle.graal.pointsto.util.AnalysisError$ParsingError: Error encountered while parsing io.grpc.netty.shaded.io.netty.util.internal.SystemPropertyUtil.getBoolean(java.lang.String, boolean)
Parsing context:
at io.grpc.netty.shaded.io.netty.util.internal.SystemPropertyUtil.getBoolean(SystemPropertyUtil.java:96)
at io.grpc.netty.shaded.io.netty.channel.epoll.Epoll.<clinit>(Epoll.java:33)
at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.AnalysisError.parsingError(AnalysisError.java:153)
...
java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)
Caused by: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of org.slf4j.jul.JDK14LoggerAdapter are allowed in the image heap as this class should be initialized at image runtime. To see how this object got instantiated use --trace-object-instantiation=org.slf4j.jul.JDK14LoggerAdapter.
Add --initialize-at-run-time=io.grpc.netty.shaded.io.netty to your META-INF/native-image/$GROUP_ID/$ARTIFACT_ID/native-image.properties file to address this issue.
However, this configuration may incur a performance cost for the generated native image, depending on the code path your application follows.
Alternatively, consider using a different logging library such as Flogger or the java.util.logging library.
Questions
Please report any issues and questions related to Java native image compilation
from the GitHub issues page.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-07 UTC."],[[["\u003cp\u003eThe Cloud Client Libraries for Java can be used to compile applications as native images, improving performance by reducing cold start-up time and memory usage through ahead-of-time compilation.\u003c/p\u003e\n"],["\u003cp\u003eTo use this feature, ensure you are using version 25.4.0 or later of the Cloud Libraries Bill of Materials (libraries-bom) or client libraries published after May 2022.\u003c/p\u003e\n"],["\u003cp\u003eNative image compilation is supported by the stable Google Cloud Client Libraries for Java listed in the Supported APIs table.\u003c/p\u003e\n"],["\u003cp\u003eYou must install GraalVM, the native image extension, and use the Google Cloud CLI to compile applications into native images using either Maven or Gradle build tools.\u003c/p\u003e\n"],["\u003cp\u003eWhen compiling client libraries with the SLF4J dependency, you might need to add the \u003ccode\u003e--initialize-at-run-time=io.grpc.netty.shaded.io.netty\u003c/code\u003e configuration to address compatibility issues.\u003c/p\u003e\n"]]],[],null,["# Compile native images\n\nUsing the Cloud Client Libraries for Java, you can compile applications as\nnative images. This approach provides performance\nbenefits to your application by leveraging ahead-of-time compilation to\nreduce the cold start-up time and memory footprint of your application.\n\nClient library versions\n-----------------------\n\nMake sure that you are using version 25.4.0 or later of the\n[Cloud Libraries Bill of Materials](/java/docs/bom) (`libraries-bom`) or that you\nare using client libraries published after May 2022.\n\nMost users manage their client library versions using the `libraries-bom`.\nThis is an easy way to check that the versions of all the client libraries in\nyour project are compatible with each other and up-to-date.\n\nFor more information about applying the `libraries-bom` to your project on Maven\nand Gradle, see [Updating your project to use the BOM](/java/docs/bom#updating_your_project_to_use_the_bom).\n\nSupported libraries\n-------------------\n\nThe stable Google Cloud Client Libraries for Java listed in the [Supported APIs\ntable](https://github.com/googleapis/google-cloud-java#supported-apis)\nsupport native image compilation.\n\nBefore you begin\n----------------\n\nTo use native image compilation with the desired Google\nCloud Client Libraries for Java, follow these prerequisite steps:\n\n1. If you have not already, create a\n [Google Cloud Project](/resource-manager/docs/creating-managing-projects).\n\n2. Install the\n [Google Cloud CLI](/sdk/docs/install),\n which allows you to run the sample with your project's credentials.\n\n3. After installing the Google Cloud CLI,\n log in with Application Default Credentials using the following command:\n\n gcloud auth application-default login\n\n | **Note:** Although authenticating with Application Default Credentials is convenient during development, we recommend [alternate methods of authentication](/docs/authentication/production) during production use.\n4. Install an appropriate JDK distribution such as\n [GraalVM](https://www.graalvm.org/latest/docs/getting-started/).\n\n5. Make sure that you installed the native image extension by running the\n following command:\n\n gu install native-image\n\n6. Verify that the default version of Java is set correctly by running\n `java -version` in a terminal.\n The output is similar to the following:\n\n java -version\n openjdk version \"17.0.9\" 2023-10-17\n OpenJDK Runtime Environment GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22)\n OpenJDK 64-Bit Server VM GraalVM CE 17.0.9+9.1 (build 17.0.9+9-jvmci-23.0-b22, mixed mode, sharing)\n\nBuild an application as a native image\n--------------------------------------\n\nThe following instructions use the native build tools plugins to build an\napplication as a native image.\n\n### Maven setup instructions\n\n1. Copy and paste this text into your application's POM to use the native\n [Maven plugin](https://graalvm.github.io/native-build-tools/latest/index.html)\n to build the native image for the application.\n Be sure to replace \u003cvar translate=\"no\"\u003eMAIN_CLASS\u003c/var\u003e with the name of your main\n class and \u003cvar translate=\"no\"\u003eVERSION\u003c/var\u003e with the version of the native Maven\n plugin, such as `0.10.1`.\n\n \u003cprofiles\u003e\n \u003cprofile\u003e\n \u003cid\u003enative\u003c/id\u003e\n \u003cbuild\u003e\n \u003cplugins\u003e\n \u003cplugin\u003e\n \u003cgroupId\u003eorg.graalvm.buildtools\u003c/groupId\u003e\n \u003cartifactId\u003enative-maven-plugin\u003c/artifactId\u003e\n \u003cversion\u003e\u003cvar translate=\"no\"\u003eVERSION\u003c/var\u003e\u003c/version\u003e\n \u003cextensions\u003etrue\u003c/extensions\u003e\n \u003cconfiguration\u003e\n \u003cmainClass\u003e$\u003cvar translate=\"no\"\u003eMAIN_CLASS\u003c/var\u003e\u003c/mainClass\u003e\n \u003cbuildArgs\u003e\n \u003cbuildArg\u003e--no-fallback\u003c/buildArg\u003e\n \u003cbuildArg\u003e--no-server\u003c/buildArg\u003e\n \u003c/buildArgs\u003e\n \u003c/configuration\u003e\n \u003cexecutions\u003e\n \u003cexecution\u003e\n \u003cid\u003ebuild-native\u003c/id\u003e\n \u003cgoals\u003e\n \u003cgoal\u003ebuild\u003c/goal\u003e\n \u003c/goals\u003e\n \u003cphase\u003epackage\u003c/phase\u003e\n \u003c/execution\u003e\n \u003c/executions\u003e\n \u003c/plugin\u003e\n \u003c/plugins\u003e\n \u003c/build\u003e\n \u003c/profile\u003e\n \u003c/profiles\u003e\n\n2. Run `mvn package -Pnative` to generate the native image in the\n `./target` folder.\n\n3. Run the application like you run any executable. For example,\n `./target/$`\u003cvar translate=\"no\"\u003eIMAGE_NAME\u003c/var\u003e. The plugin defaults to the artifact\n ID of the project.\n\n### Gradle setup instructions\n\n1. Set the main class of your application by pasting the following text in\n your `build.gradle` file.\n\n application {\n mainClass.set('$\u003cvar translate=\"no\"\u003eMAIN_CLASS\u003c/var\u003e')\n }\n\n2. Add the native-gradle plugin as described in\n [Adding the plugin](https://graalvm.github.io/native-build-tools/latest/gradle-plugin.html#_adding_the_plugin).\n\n3. Call `./gradlew nativeCompile` to build the native image into the\n `./build/native/nativeCompile` directory.\n\n4. Run the application as an executable. For example,\n `./build/native/nativeCompile/$`\u003cvar translate=\"no\"\u003ePROJECT_NAME\u003c/var\u003e.\n With the plugin, the image name defaults to the project name.\n\nCommon Issues\n-------------\n\n### SLF4J compatibility\n\nIf you are performing native image compilation on a client library with the [SLF4J](https://www.slf4j.org/) dependency, you may see the following built-time error. \n\n Fatal error: com.oracle.graal.pointsto.util.AnalysisError$ParsingError: Error encountered while parsing io.grpc.netty.shaded.io.netty.util.internal.SystemPropertyUtil.getBoolean(java.lang.String, boolean)\n Parsing context:\n at io.grpc.netty.shaded.io.netty.util.internal.SystemPropertyUtil.getBoolean(SystemPropertyUtil.java:96)\n at io.grpc.netty.shaded.io.netty.channel.epoll.Epoll.\u003cclinit\u003e(Epoll.java:33)\n at org.graalvm.nativeimage.pointsto/com.oracle.graal.pointsto.util.AnalysisError.parsingError(AnalysisError.java:153)\n ...\n java.base/java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:165)\n Caused by: com.oracle.graal.pointsto.constraints.UnsupportedFeatureException: No instances of org.slf4j.jul.JDK14LoggerAdapter are allowed in the image heap as this class should be initialized at image runtime. To see how this object got instantiated use --trace-object-instantiation=org.slf4j.jul.JDK14LoggerAdapter.\n\nAdd `--initialize-at-run-time=io.grpc.netty.shaded.io.netty` to your `META-INF/native-image/$`\u003cvar translate=\"no\"\u003eGROUP_ID\u003c/var\u003e`/$`\u003cvar translate=\"no\"\u003eARTIFACT_ID\u003c/var\u003e`/native-image.properties` file to address this issue.\n\nHowever, this configuration may incur a performance cost for the generated native image, depending on the code path your application follows.\n\nAlternatively, consider using a different logging library such as [Flogger](https://google.github.io/flogger/) or the java.util.logging library.\n\nQuestions\n---------\n\nPlease report any issues and questions related to Java native image compilation\nfrom the [GitHub issues page](https://github.com/googleapis/google-cloud-java/issues).\n\nWhat's next?\n------------\n\n- [Review the native image sample project using Secret Manager](https://github.com/googleapis/google-cloud-java/tree/main/java-samples/native-image-sample)."]]