Enabling Java Preview Features in IntelliJ

A Step-by-Step Guide to Unlock Java Experimental Functionality in IntelliJ

Razvan Badescu
Javarevisited
Published in
4 min readJul 17, 2023

--

My dog curious about tech
Lucy enabling technology

If you’re someone who enjoys exploring experimental features, you might be interested in trying out Java’s preview features. However, when you attempt to run your code, you may encounter an error requesting the use of the “ — enable-preview” VM option.

Error e.g.

Error: LinkageError occurred while loading main class Main
java.lang.UnsupportedClassVersionError: Preview features are not enabled for Main (class file version 65.65535). Try running with '--enable-preview'

In this article, we’ll walk you through the process of setting up your IntelliJ IDE to enable the preview feature, so you can harness the power of these experimental functionalities.

  1. Set the experimental JDK version for your project

a) To begin, go to “File” > “Project Structure” and select a JDK that includes experimental features. Make sure the project’s language level is set to the default level, which also incorporates experimental features

A capture from IntelliJ Project Structure section, highlighting setting the JDK that contains experimental features
Setting the Project JDK

b) Set the Modules language level to default to project’s language level

Modules Language Level

c) Under “Platform Settings“ >”SDKs” set the JDK:

2. Configure the Java Compiler
Next, access the Java Compiler settings by going to “File” > “Settings” and searching for “Java Compiler”. Here, you’ll need to add the “ — source” and “ — enable-preview” command line arguments to your project.

E.g. --enable-preview --source 21

Capture that shows how to add — enable-preview and — sourse as command line arguments for Java Compiler in IntelliJ
Enable Preview Mode on the Java Compiler

3. Add the “ — enable-preview” VM Option
To finalize the configuration, locate the configurations of your main class, and if not already present, select “Add VM options.” Add the “ — enable-preview” option to enable preview features for your project.

Capture of the Edit Configuration IntelliJ Section
Edit Configuration
Capture showing how to add VM Options section to the main class configuration in IntelliJ
Add the VM Options Section
Capture showing how to add the — enable-preview VM Option in Intellij
Set the “— enable-preview” as VM Option

4. Configure Maven

If your project uses Maven as build tool, you have to set source and target to the language level:

<properties>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
</properties>

Additionally, you can explicitly configure the maven compiler plugin to set the source, target and the — enable-preview argument:

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.11.0</version>
<configuration>
<source>21</source>
<target>21</target>
<encoding>UTF-8</encoding>
<compilerArgs>
<compilerArg>--enable-preview</compilerArg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>

⚠️Note: If you’re using an older IntelliJ IDE, the latest JDK versions (like 21) might not be supported. The solution would be to use the latest community or ultimate version of it.

5. Enable Preview From Command Line

Use this option only if you prefer to specifically enable preview for an application and run it directly from command line.

  • Compile your app:
javac --release 21 --enable-preview MyApp.java
  • Run it:
java --enable-preview MyApp

That’s it! You’re now well-versed and equipped to explore the preview features and leverage their potential.

Conclusion

Enabling the preview feature in IntelliJ allows you to take advantage of Java’s experimental functionalities. By following the simple steps outlined in this guide, you can set up your IntelliJ IDE to accommodate these features and unlock new possibilities in your coding journey.

Enjoy exploring the realm of Java preview features and discover innovative ways to enhance your development experience.

👏If you enjoyed reading this article, don’t forget to give it a round of applause to show your appreciation.

🔔 Follow me to stay updated with more insightful and practical articles.

--

--