How to Enable Java JDK 21 Preview Features on IntelliJ

If you make use of the Java JDK 21 Preview Features in IntelliJ IDE you will see a compilation error, and also when you execute such code, you will see errors.

/Users/code2care/IdeaProjects/demo/src/Main.java:7:25

java: string templates are a preview feature and are disabled by default.
  (use --enable-preview to enable string templates)

To enable preview features of Java JDK, you can follow the below step.

  1. Open Project Settings by pressing Command ⌘ + ; on Mac, or Ctrl+Alt+Shift+S if on Windows or Linux.
  2. Now for Language level: select "21 (Preview) - String templates, unnamed classes and instance main methods etc."
  3. You will see Experimental Feature Alert
  4. You must accept the terms of legal notice of the beta Java specification to enable support for "21 (Preview) - String templates, unnamed classes and instance main methods etc.".

    implementation of an early-draft specification developed under the Java Community Process (JCP) is made available for testing and evaluation purposes only and is not compatible with any specification of the JCP.

    Click on Accept.

  5. Close the Project Structure window.
  6. You will see that the complications errors are gone, and no errors in the console when you run the Java programs with preview features.

/Users/code2care/Downloads/jdk-21.jdk/Contents/Home/bin/java 
--enable-preview -javaagent:/Applications/IntelliJ 
IDEA.app/Contents/lib/idea_rt.jar=51446:/Applications/IntelliJ IDEA.app/Contents/bin 
-Dfile.encoding=UTF-8 -Dsun.stdout.encoding=UTF-8 
-Dsun.stderr.encoding=UTF-8 -classpath /Users/code2care/IdeaProjects/demo/out/production/demo Main

Hello World from JDK 21!

21 (Preview) - String templates, unnamed classes and instance main methods etc

Comments & Discussion

Facing issues? Have questions? Post them here! We're happy to help!