How to try Java preview features (such as Java 15 Record) in IntelliJ

Conclusion

  1. (Update to the latest version of IntelliJ IDEA)
  2. Set the project to use the relevant JDK
  3. Download the JDK you want to use
  4. Turn on the preview function 2. File>Project Structure>Project Settings>Modules
  5. Select " 15 (Preview) --Sealed types, records, patterns, local enums and interfaces "in Language level

environment

Detailed procedure

1. Update to the latest version of IntelliJ IDEA

New features in Java 15 require IntelliJ newer than 2020.2. Click here for 2020.2 update information If the version is old, please update it by the following method.

Automatic update

You can update automatically from Help> Check for Updates ... Press the ʻupdate and restart` button and wait and you're done.

Manual update

Download and update the specified version of the file from here You can also. The installation wizard will ask you if you want to erase the old version and keep the settings.

2. Set the project to use the relevant JDK

Open IntelliJ and create a new project. image.png If you select Download JDK from the pull-down, the selected distribution and version of the JDK will be set as the JDK used in your project. image.png image.png If you want to use the Early Access version that is not displayed here, please specify the JDK downloaded by "Add JDK ..." after downloading by yourself. (Specify the directory where bin and lib are located.) image.png

3. Turn on the preview function

Open File> Project Structure> Project Settings> Modules. image.png From the Language level pull-down, specify the one that matches the function you want to use. Since I will try records this time, I specified " 15 (Preview) --Sealed types, records, patterns, local enums and interfaces ". image.png

If Java 15 is not set, please check the IntelliJ version and Java version again.

Try to run

First, create a Record class.

package records;

public record ExampleRecord(String name, int age) {
}

Next, create a Main class to execute.

package main;

import records.ExampleRecord;

public class Main {
  public static void main(String[] args) {
    ExampleRecord exampleRecord = new ExampleRecord("test", 20);
    System.out.println(exampleRecord);
  }
}

When I run it, it looks like this!

{JDK_path}\bin\java.exe --enable-preview "-javaagent:{IntelliJ IDEA_path}\lib\idea_rt.jar=50507:{IntelliJ IDEA_path}\bin" -Dfile.encoding=UTF-8 -classpath {workspace_path}\{project_name}\out\production\record_refrection main.Main
ExampleRecord[name=test, age=20]

The point is that --enable-preview is given as an option. This allows you to use the preview function Record. That's it.

Recommended Posts

How to try Java preview features (such as Java 15 Record) in IntelliJ
How to use ArgumentMatchers such as Mockito's any () in Kotlin
How to learn JAVA in 7 days
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
How to concatenate strings in java
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
How to do base conversion in Java
How to implement coding conventions in Java
How to embed Janus Graph in Java
How to get the date in java
How to call libraries such as JQuery and JQuery UI in Liferay 7 / DXP
How to display a web page in Java
Try to create a bulletin board in Java
How to get Class from Element in Java
How to hide null fields in response in Java
[Java] How to substitute Model Mapper in Jackson
How to solve an Expression Problem in Java
How to write Java String # getBytes in Kotlin?
How to call functions in bulk with Java reflection
How to create a Java environment in just 3 seconds
[Java] How to omit the private constructor in Lombok
How to input / output IBM mainframe files in Java?
How to use java non-standard library on IntelliJ IDEA
How to create a Spring Boot project in IntelliJ
How to create a data URI (base64) in Java
How to display a browser preview in VS Code
As of April 2018 How to get Java 8 on Mac
How to generate / verify ID token in Java Memo
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
How to Git manage Java EE projects in Eclipse
Try to solve a restricted FizzBuzz problem in Java
Summary of how to implement default arguments in Java
How to put old Java (8 series) in macOS 10.15 Catalina
Notes on how to use regular expressions in Java
How to get the class name / method name running in Java
How to use JSON data in WebSocket communication (Java, JavaScript)
Memo: [Java] How to check groupId etc. described in pom.xml
How to store a string from ArrayList to String in Java (Personal)
What happened in "Java 8 to Java 11" and how to build an environment
How to call and use API in Java (Spring Boot)
How to use Java enums (Enum) in MyBatis Mapper XML
How to dynamically switch JDK when building Java in Gradle
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
How to derive the last day of the month in Java
Differences in how to handle strings between Java and Perl
How to switch Java in the OpenJDK era on Mac
[Java] How to use Map
Try using RocksDB in Java
How to lower java version
[Java] How to use Map
How to uninstall Java 8 (Mac)
Java --How to make JTable