GraalVM, a polyglot virtual machine that provides a shared runtime to execute applications written in multiple languages like Java, C, Python, and JavaScript, has released version 19.3 with support for JDK 11. Previous versions of GraalVM were based on JDK 8.
The numerous language features and platform improvements like compact strings, variable type inference, Java Platform Module System (JPMS), and HTTP client, that were delivered between the Java 9 release and Java 11 release can now be used by JVM applications built on GraalVM.
For example, the following snippet shows how a simple request to a bitcoin REST API can be constructed and invoked using the new Java 11 HTTP client:
public class BPI {
public static void main(String... args) {
var request = HttpRequest
.newBuilder()
.uri(URI.create("https://api.coindesk.com/v1/bpi/currentprice.json"))
.GET()
.build();
var response = HttpClient
.newHttpClient()
.send(request, HttpResponse.BodyHandlers.ofInputStream());
var json = Json
.createReader(response.body())
.readObject()
.getJsonObject("bpi");
…
System.out.printf("Current Bitcoin Price: $%s %s", price, indicator);
}
}
With JAVA_HOME
and PATH
pointing to a GraalVM installation, Java 11 based classes like the example class above can be compiled using the javac
compiler or a build tool like Maven.
GraalVM’s native image generation utility has also been updated to support Java 11 as an early adopter technology feature. A native image is an ahead-of-time compiled Java bytecode packaged as a standalone executable. The native image typically achieves faster startup time and smaller footprint. Note that the native image does not support Java Platform Module System and has no module introspection at image runtime. The native-image
utility is an optional package that can be installed using the GraalVM Updater, gu. gu
is a package manager that downloads and installs packages not included in the core distribution of GraalVM.
The following snippet shows how to install GraalVM native-image
utility, build a native image based on the above mentioned example class, and run the generated standalone executable:
gu install native-image
native-image -cp example.jar \
--enable-url-protocols=https \
my.example.BPI
For those who use Maven as the build tool, a Maven plugin is provided to assist in the image generation process.
Also note that since Garbage-First (G1) garbage collector is the default garbage collector in Java 11, care must be taken while migrating performance sensitive applications from JDK 8 based GraalVM to JDK 11 based GraalVM.
In addition to JDK 11 support, this first long-term support (LTS) release of GraalVM provides improvements to native image generation, GraalVM compiler, tooling like code coverage and GraalVM VisualVM along with runtime upgrades of NodeJS, Python, and R languages. The release notes describe the full list of improvements and enhancements delivered in GraalVM 19.3 release.
Resources
- Announcing GraalVM 19.3 with JDK 11 Support by GraalVM Team (November 20, 2019)
- GraalVM 19.0: the Anticipated GA Release by InfoQ (July 17, 2019)
- Java Feature Spotlight: Local Variable Type Inference by Brian Goetz (November 1, 2019)
- Getting to Know Graal, the New Java JIT Compiler by Ben Evans (July 16, 2018)