1. Introduction
Preview features in Java allow developers to experiment with new language enhancements before they are finalized. These features are fully specified and implemented, but not yet permanent parts of the Java platform. If you’re working with a Long-Term Support (LTS) version like JDK 17, you may encounter powerful additions such as pattern matching for switch
, which are only accessible as preview features. In this article, you’ll learn how to enable these features in your local setup.
⚠️ Caution: Preview features are fully implemented but not finalized and should never be used in production environments.
2. Enable Preview Features via Command Line
To compile and run code that uses preview features:
🛠 Compile
javac --release 17 --enable-preview EnablePreviewFeaturesDemo.java
This will compile the class EnablePreviewFeaturesDemo in Java 17 and enable preview features. Hence, the pattern matching for switch will compile properly.
▶️ Run
java --enable-preview EnablePreviewFeaturesDemo
3. Enable Preview Features in IntelliJ IDEA
Step-by-Step
- Go to
File > Project Structure > Project
- Set Project SDK to JDK 17
- Set Language level to 17 (Preview features)
- Go to
File > Project Structure > Modules
- Set module language level to 17 (Preview features)
- Go to
Run > Edit Configurations
- Click Modify options (top-right)
- Enable VM options
- In the VM options field, add:
--enable-preview
- Rebuild the project
4. Enable Preview Features in Maven
In case you have a Maven project, update the build section of the pom.xml to enable preview features.
Update your pom.xml
like this:
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
<configuration>
<argLine>--enable-preview</argLine>
</configuration>
</plugin>
</plugins>
</build>
5. Conclusion
Preview features allow you to explore the future of Java today — but they require a bit of setup. Whether you’re compiling from the command line, working in IntelliJ, or building with Maven, enabling preview features gives you access to the latest tools and syntax enhancements.
You can find the complete code of this article here in GitHub.