How to run Java preview features such as JDK 13 text blocks

With each new Java platform release, the stewards of the language always include a few new preview features that can be toyed with only if a developer knows how to unlock the capabilities at runtime.

Let's explore how you can enable preview features in Java 13 and subsequently demonstrate how to use a multi-line String literal in your code. This Java preview feature is known as the JDK 13 text block.

Step 1: Install the JDK

If you want to play around with a Java preview feature, you'll first need a Java Development Kit. Here's how to install JDK 13 on Ubuntu and set the all-important JAVA_HOME variable.

Step 2: Write code that uses a Java preview feature

The Java preview feature I'd like to test is the JDK 13 text block semantics. Java text blocks allow developers to write Java Strings that span multiple lines of code. JDK 13 text blocks are delineated by three consecutive double quotes. A simple example of an executable Java class that uses this Java preview feature would be:

Java text blocks
Code that uses the JDK 13 text blocks Java preview feature.

Step 3: Compile the Java preview feature code

If you attempt to compile this code with the standard semantics of the javac command line tool, you'll encounter a compile time error that states "text blocks are a preview feature and are disabled by default."

Java preview error
A compile time error caused by the compilation code that uses a Java preview feature.

Java preview compile time switches

To eliminate this compile time error, just add a few switches to the javac command, which indicates that you are well aware that the code you want to compile uses features that may or may not be supported in the future. Those switches are:

  • --release 13
  • --enable-preview
  • -Xlint:preview

The command line code looks like this:

jdk13@preview:$ javac --release 13 --enable-preview -Xlint:preview EnableJavaPreviewFeatures.java

Step 4: Ignore the compile time warning

When the code compiles, the Java compiler will generate a warning message that looks disconcertedly like an error message. The warning message reads:

warning: [preview] text blocks are a preview feature and may be removed in a future release.

Despite the warning, the code has successfully compiled, and bytecode has been generated on the file system. You can ignore this warning and move on to the next step.

Step 5: Run the Java preview feature code

With the code compiled, you can execute it by adding the --enable-preview switch to the java command:

jdk13@preview:$ java --enable-preview EnableJavaPreviewFeatures

When the code runs, the content of the JDK 13 text block will output to the terminal window.

JDK 13 program
Successful compilation and execution of code that uses a Java preview feature.

Steps to run Java preview features

In summary, the five steps involved in the compilation and execution of source code that uses a Java preview feature are:

  1. Ensure the JDK of interest is installed.
  2. Write code that enacts the Java preview feature.
  3. Compile the code using the required switches.
  4. Ignore the preview feature compilation warning.
  5. Run the code using the -- enable-preview switch.

And that's all there is to know about how to use Java preview features such as JDK 13 text blocks.

Example source code

The code used in this example is:

public class EnableJavaPreviewFeatures {
   public static void main(String[] args) {
      /* JDK 13 Text Blocks example */
      String textBlocks = """
         <html>
           <body>
              <h1>Text Blocks</h1>
              <p>A Java 13 Preview Feature</p>
           </body>
        </html>""";
   System.out.println(textBlocks);
   }
}

View All Videos
App Architecture
Software Quality
Cloud Computing
Security
SearchAWS
Close