How To Enable Previews In Java » The Bored Dev

How To Enable Previews in Java

If you are a developer using any of the JVM languages, you must have seen that sometimes JDK releases include new proposals in the form of a “preview“. In order to be able to use them, you will have to enable previews in Java.
Previews have been available for a few years only after the introduction of JEP 12, introducing the so-called “preview features“.

The intent of this previews is to give developers a chance to provide feedback about a new feature, always keeping in mind that this new feature is not final and it’s subject to changes after receiving suggestions or being notified about issues by developers. Therefore we’ll have to remind you NOT to enable these previews in production, as they’re not safe or stable.

The question then is: How can we enable previews in Java?

Enabling previews

There are a couple of ways to enable preview features. Let’s see how!

Running a jar

If we’re running a jar like the one that Spring Boot normally generates for us, you could run it enabling preview features in this way:

java --enable-preview -jar my-app/my-app-1.0.0.jar

In this type of jars the main class is normally defined in the MANIFEST, that’s why we don’t need to specify an entrypoint.

Running a java class

Running a single Java class is also quite simple, you’d have to do something similar to this:

java --enable-preview --release 19 MainApp.java

These are the two types of command-line arguments we could use but what about enabling it from our IDE? Let’s take a look!

From IntelliJ IDE

In IntelliJ we can enable preview features by setting the compiler level to a preview version. To do so we should go to Project Structure... first:

How To Enable Previews In Java » The Bored Dev
Image Credit: Author

Once we are in the project structure view we have to click on the language level dropdown list and select one of the preview versions, in our screenshot we are selecting Java 17 in preview mode.

How To Enable Previews In Java » The Bored Dev
Image Credit: Author

If you’re building the application and running tests using Gradle on your IDE, you should keep in mind that the JDKs should match between the two. Make sure that you select Project SDK in Gradle settings in your Preferences , as shown below:

How To Enable Previews In Java » The Bored Dev
Image Credit: Author

Let’s look now at achieving this by using Gradle configuration directly!

From Gradle configuration

To enable preview features from Gradle we could pass the argument to the compileJava task by doing the following:

apply plugin: 'java'

compileJava {
    options.compilerArgs << '--enable-preview' 
}

Conclusion

We’ve seen different ways to enable preview features in Java. If you think there are other ways that could be useful to others, please get in touch and we’ll try to add them.

Also please remember that you should NEVER enable previews in production environments.

That’s all from us today, I hope you found this article useful and we’ll hope to see you back soon!

Please follow us if you’re interested in reading more interesting articles!