This article shows you how to enable preview feature in Java.

–enable-preview to enable text blocks:

To enable the preview feature, make sure you are using java 12 or later version. Even if you are using Java 12 or above versions you might get “Use --enable-preview to enable text blocks” error because the text blocks and preview feature are disabled by default. We have to enable them manually.

–enable-preview Intellij:

In intellij goto File->Project Structure-> Project and makesure the below selected items

--enable-preview to enable text blocks

–enable-preview terminal:

# compiling
javac --release 14 --enable-preview Demo.java  // Enable all preview features of JDK 12

#Run
java --enable-preview Demo      // Run with preview features of JDK 14

–enable-preview maven:

Add the below maven plugin in pom.xml to enable-preview

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <version>3.8.0</version>
      <configuration>
        <release>14</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>

–enable-preview gradle:

For gradle you need to configure the javaCompile task, so that Gradle passes this option to the Java compiler when compiling the java classes. Add the below code in build.gradle file

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

References:

Happy Learning 🙂

About the Author:

Founder of onlinetutorialspoint.com Love Java, Python, Shell and opensource frameworks. Follow him on twitter and facebook for latest updates.

Leave A Comment