Fix: java: string templates are a preview feature and are disabled by default.


Java JDK 21 Code Example:

import static java.lang.StringTemplate.STR;

public class Main {
    public static void main(String[] args) {
        String name = "Mike";
        System.out.printf(STR."Hello, my name is \{name}");
    }
}
/Users/code2care/IdeaProjects/demo/src/Main.java:10:31

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

If you make use of features such "STR - String Template", which are still in Preview in Java JDK 21, then you will see compilation errors, and when you run the program you will see a message saying that the "preview feature and are disabled by default".

If you still want to make use of it then you will need to pass a flag --enable-preview and --source followed by the JDK version when you run the java/javac command on Terminal.

Macbook % java --enable-preview --source 21 Main.java

Note: Main.java uses preview features of Java SE 21.
Note: Recompile with -Xlint:preview for details.

Hello, my name is Mike
Java - Fix - string templates are a preview feature and are disabled by default

Facing issues? Have Questions? Post them here! I am happy to answer!

Author Info:

Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.

Follow him on: X

You can also reach out to him via e-mail: rakesh@code2care.org

Copyright © Code2care 2024 | Privacy Policy | About Us | Contact Us | Sitemap