Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enable Java 12 preview features with Gradle?

Tags:

When I tried to build my Java code which has switch expressions using Gradle, it throws this error:

error: switch expressions are a preview feature and are disabled by default. 

I tried running ./gradlew build --enable-preview which didn't work either.

I'm using Gradle 5.3.1.

like image 679
Murali Krishna Avatar asked Mar 30 '19 17:03

Murali Krishna


People also ask

How do I enable Java preview?

First, you need to go preference, and then Build, Execution, Deployment and then Select Java Compiler. And then go to the run configuration. Select the modify options and Mark the Add VM options. You need to add --enable-preview there as well.

Is gradle compatible with Java 11?

The minimum version of Gradle that supports Java 11 is 5.0 . You would need to upgrade to version 7.0 or above for Android.


1 Answers

You need to configure the JavaCompile tasks, so that Gradle passes this option to the Java compiler when compiling.

Something like this should work:

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

To run the app/tests we need to add jvmArgs.

Example:

test {     jvmArgs(['--enable-preview']) } 
like image 130
JB Nizet Avatar answered Nov 28 '22 10:11

JB Nizet