Oracle GraalVM in OCI Code Editor
This guide shows you how to get started with Oracle GraalVM in Oracle Cloud Infrastructure (OCI) Code Editor.
OCI Code Editor provides a rich, in-console editing environment that enables you to edit code without having to switch between the Oracle Cloud Console and your local development environment. The Code Editor enables you to edit and deploy code for OCI services directly from the OCI Console.
Oracle GraalVM for JDK 17 is preinstalled in Cloud Shell, so you do not have to install and configure a development machine. Code Editor’s integration with Cloud Shell gives you direct access to it.
Create and Run a Java Application in OCI Code Editor
Step 1: Open a Terminal in Code Editor
- Login to the Oracle Cloud Console and launch Code Editor.
- Open a Terminal in Code Editor, by clicking New Terminal from the Terminal menu.
Step 2: Select GraalVM JDK as the Default JDK
- List the installed JDKs using the
csruntimectl java listcommand.csruntimectl java listThe output lists the JDKs preinstalled in Cloud Shell: Oracle GraalVM for JDK 17, Oracle JDK 11, and Oracle JDK 8. The JDK marked with an asterisk is the current JDK.
- Select Oracle GraalVM for JDK 17 as the current JDK:
csruntimectl java set graalvmjdk-17You will see the confirmation message printed: “The current managed java version is set to graalvmjdk-17”.
- Now confirm the values of the environment variables
PATHandJAVA_HOME, and the versions ofjavaand thenative-imagetool:echo $JAVA_HOMEecho $PATHjava -versionnative-image --version
Step 3: Setup a Java Project and Run
- Clone a demo repository and open it in OCI Code Editor. To achieve this, run the following commands one by one:
git init graalvmee-java-hello-worldcd graalvmee-java-hello-worldgit remote add origin https://github.com/oracle-devrel/oci-code-editor-samples.gitgit config core.sparsecheckout trueecho "java-samples/graalvmee-java-hello-world/*">>.git/info/sparse-checkoutgit pull --depth=1 origin maincd java-samples/graalvmee-java-hello-world/You can now view/edit the sample code in Code Editor.
- Package the sample application into a runnable JAR file:
mvn clean package - Run the JAR file:
java -jar target/my-app-1.0-SNAPSHOT.jarIt prints out “Hello World!”.
Step 4: Build and Run a Native Executable
This Java application incorporates the Maven plugin for GraalVM Native Image that adds support for building native executables using Apache Maven. For testing purposes, build a native executable with the quick build mode first enabled and then disabled.
Quick Build Mode Enabled
- To enable the quick build mode, uncomment this line in pom.xml, as follows:
<quickBuild>true</quickBuild> - Build a native executable using the
nativeMaven profile:mvn clean -Pnative -DskipTests packageThis will generate a native executable for Linux in the target directory, named my-app.
- Run the app native executable in the background:
./target/my-app
Quick Build Mode Disabled
- To disable the quick build mode, comment out this line in pom.xml, as follows:
<!-- <quickBuild>true</quickBuild> --> - Build a native executable again:
mvn clean -Pnative -DskipTests packageThis will generate a native executable, my-app, in the target directory, replacing the previous one. You have probably noticed how the quick build mode reduced the time required to generate a native executable, making it easier to use Native Image in a typical development cycle (compile, test, and debug). However, the size of a generated executable is larger and peak performance is worse. The quick build mode is recommended for development purposes only.
- Run the native executable:
./target/my-app
Congratulations! You have successfully built and run a native executable using Oracle GraalVM in OCI Code Editor without the need to switch between the Oracle Cloud Console and your local development environments. The Code Editor allows you to accomplish quick coding tasks and run applications directly from the OCI Console.