GraalVM demos: Native images for faster startup
This is a sample application to demonstrate GraalVM capabilities for creating native images.
Download or clone the repository and navigate into the
git clone https://github.com/graalvm/graalvm-demos cd graalvm-demos/native-list-dir
There are two Java classes, but we will start by building
for the purposes of this demo. You can manually execute
there is also a
build.sh script included for your convenience.
Note that you can use any JDK for compiling the Java classes, however we refer
javac from GraalVM in the build script to simplify the prerequisites
and not to depend on another JDK installed.
Export the GraalVM home directory as the
$GRAALVM_HOME and add
to the path, using a command-line shell for Linux:
and for Mac OS:
Note that your paths are likely to be different depending on the download location.
build.sh script creates a native image from the Java class.
Let us look at it in more detail:
native-image utility is a part of GraalVM. It is used to compile
applications ahead-of-time for faster starup and lower general overhead at runtime.
After executing the
native-image command, check the directory,
it should have produced an executable file
Running the application
To run the application, you need to either execute the
as a normal Java application using
java, or, since we have a native image
prepared, run that directly.
run.sh file, executes both, and times them with the
time java ListDir $1 time ./listdir $1
To make it more interesting, pass it to a parent directory:
./run.sh .. (
.. - is
the parent of the current directory, the one containing all the demos).
Approximately, the following output should be produced:
+ java ListDir .. Walking path: .. Total: 141 files, total size = 14448801 bytes real 0m0.320s user 0m0.379s sys 0m0.070s + ./listDir .. Walking path: .. Total: 141 files, total size = 14448801 bytes real 0m0.030s user 0m0.005s sys 0m0.011s
The performance gain of the native version is largely due to the faster startup.
You can also experiment with a more sophisticated
To compile that class you need to add
graal-sdk.jar on the classpath:
$GRAALVM_HOME/bin/javac -cp $GRAALVM_HOME/jre/lib/boot/graal-sdk.jar ExtListDir.java
native-image utility about it by passing the
$GRAALVM_HOME/bin/native-image --language:js ExtListDir
The execution is the same as in the previous example:
time java ExtListDir $1 time ./extlistdir $1