This example shows how you can get started quickly with Oracle GraalVM in Oracle Cloud Infrastructure (OCI) Cloud Shell. This example uses a simple hello world REST application built with the Micronaut framework and GraalVM.
GraalVM is an advanced JDK with ahead-of-time Native Image compilation. GraalVM Native Image ahead-of-time compilation builds your Java application into a native executable that is small, starts fast, and uses less memory and CPU. Leading Java microservices frameworks such as Spring Boot, Micronaut, Quarkus and Helidon support GraalVM Native Image.
Oracle GraalVM is available for use on Oracle Cloud Infrastructure (OCI) at no additional cost.
Micronaut is a modern, JVM-based framework to build modular, easily testable microservice and serverless applications. By avoiding runtime reflection in favor of annotation processing, Micronaut improves the Java-based development experience by detecting errors at compile time instead of runtime, and improves Java-based application start time and memory footprint. Micronaut includes a persistence framework called Micronaut Data that precomputes your SQL queries at compilation time making it a great fit for working with databases like MySQL, Oracle Autonomous Database, etc.
Micronaut uses GraalVM Native Image to build lightweight Java applications that use less memory and CPUs, are smaller and faster because of GraalVM's advanced ahead-of-time compilation technology.
Cloud Shell is a free-to-use browser-based terminal accessible from the Oracle Cloud Console. It provides access to a Linux shell with preinstalled developer tools and a preauthenticated OCI CLI. You can use the shell to interact with OCI resources, follow labs and tutorials, and quickly run utility commands.
Oracle GraalVM for JDK 17 (with Native Image) is preinstalled in Cloud Shell, so you don’t have to install and configure a development machine to get started.
-
List the installed JDKs:
csruntimectl java list
The output should be similar to (versions may vary):
graalvmjdk-17 /usr/lib64/graalvm/graalvm-java17 * oraclejdk-11 /usr/java/jdk-11.0.17 oraclejdk-1.8 /usr/lib/jvm/jdk-1.8-oracle-x64
-
Select GraalVM as the current JDK:
csruntimectl java set graalvmjdk-17
The output should be similar to:
The current managed java version is set to graalvmjdk-17.
-
Git clone this example.
git init graalvm-mn-hello-rest-maven cd graalvm-mn-hello-rest-maven git remote add origin https://github.com/graalvm/graalvm-demos.git git config core.sparsecheckout true echo "micronaut-hello-rest-maven/*">>.git/info/sparse-checkout git pull --depth=1 origin master cd micronaut-hello-rest-maven
-
[OPTIONAL] Confirm software version and environment variables.
-
Build a JAR file for the example app
./mvnw package
-
Run the JAR in the background
java -jar target/MnHelloRest-0.1.jar &
-
Test the JAR
5.1) Should output "Hello World"
curl http://localhost:8080/
5.2) Should output "Hello Micronaut"
curl http://localhost:8080/Micronaut
-
Bring the running JAR in the foreground
fg
-
Once the app is running in the foreground, press CTRL+C to stop it.
Use GraalVM Native Image to produce a native executable.
-
Build the app native executable
./mvnw package -Dpackaging=native-image
-
Run the app native executable in the background
./target/MnHelloRest &
-
Test the app native executable
3.1) Should output "Hello World"
curl http://localhost:8080/
3.2) Should output "Hello Micronaut-Graal-Native"
curl http://localhost:8080/Micronaut-Graal-Native
-
Bring the running app JAR in the foreground
fg
-
Once the app is running in the foreground, press CTRL+C to stop it.