Main Tutorials

Java – How to enable the preview language features?

This article shows you how to use --enable-preview to enable the preview language features in Java 12, 13 and above.

P.S All preview features are disabled by default.

On JDK 12:


# compile
javac Example.java                                // Do not enable any preview features
javac --release 12 --enable-preview Example.java  // Enable all preview features of JDK 12
javac --release 11 --enable-preview Example.java  // DISALLOWED

#Run
java --enable-preview Example					  // Run with preview features of JDK 12

On JDK 13:


javac Example.java                                // Do not enable any preview features
javac --release 13 --enable-preview Example.java  // Enable all preview features of JDK 13
javac --release 12 --enable-preview Example.java  // DISALLOWED

java --enable-preview Example					  // Run with preview features of JDK 13

1. Maven

Enable preview features in Maven.

pom.xml

 <build>
	<plugins>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-compiler-plugin</artifactId>
			<version>3.8.0</version>
			<configuration>
				<!-- 1.10, 11, 12, 13-->
				<release>13</release>
				<compilerArgs>
					--enable-preview
				</compilerArgs>
			</configuration>
		</plugin>
		<plugin>
			<groupId>org.apache.maven.plugins</groupId>
			<artifactId>maven-surefire-plugin</artifactId>
			<version>3.0.0-M3</version>
			<configuration>
				<argLine>--enable-preview</argLine>
			</configuration>
		</plugin>
	</plugins>
 </build>

2. Gradle

For Java 12, enable preview features in Gradle.


	tasks.withType(JavaCompile).each {
		it.options.compilerArgs.add('--enable-preview')
	}

	test {
		jvmArgs(['--enable-preview'])
	}
Note
Gradle 5.6.2 and below is not working with the latest Java 13, see this issue , wait Gradle 6.0 to fix it.

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments