Helidon & Java SE Preview Features Cheat Sheet

Posted on October 27, 2020


I often use Helidon to demo new Java SE features, including Preview Features (ex. Record classes, Sealed classes, etc). This simple cheat sheet explains how to use Java SE features with Helidon.


book cover Photo by Paul Skorupskas


As a reminder, Java SE Preview Features should explicitly be enabled at both compile-time and run-time. This is done by simpling passing the --enable-preview flag to the javac compiler, or to the JVM. For more details on Java SE Preview Features, check this article.

Compile-time

To enable Preview Features at compile-time, update the Helidon project’s pom.xml. Make sure to specify the Java version you are using.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-compiler-plugin</artifactId>
   <version>3.8.0</version>
   <configuration>
      <compilerArgs>--enable-preview</compilerArgs>
      <release>16</release>
   </configuration>
</plugin>

Testing

To enable Preview Features during tests, update the Helidon project’s pom.xml.

<plugin>
   <groupId>org.apache.maven.plugins</groupId>
   <artifactId>maven-surefire-plugin</artifactId>
   <version>3.0.0-M5</version>
   <configuration>
      <argLine>--enable-preview</argLine>
   </configuration>
</plugin>

Run-time

If the Helidon application is launched from the command line, simply pass the flag to the JVM.

java --enable-preview -jar target/bare-se.jar

-

If you are using the Helidon CLI DevLoop, pass the argument to the JVM when starting the DevLoop.

helidon dev --app-jvm-args "--enable-preview"

-

If you are using the Maven plugin to generate, via jlink a custom Java runtime image for your Helidon application, you can specify argument(s) that should be passed to the JVM.

mvn package -Pjlink-image -Djlink.image.defaultJvmOptions="--enable-preview"

The Helidon application can then be lanched via the generated script.

target/bare-se-jri/bin/start

-

If the Helidon application is containerized, just update its Dockerfile.

…
CMD ["java", "--enable-preview", "-jar", "bare-se.jar"]

~