Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Enabling Java preview features

Roland Grunberg edited this pageNov 4, 2024 ·10 revisions

Under the hood, instead ofjavac, vscode-java usesECJ, a Java 23 compiler provided byEclipse JDT. It is used to compile against all other versions of Java.But the ECJ compileronly supports the latest JDK release when it comes to preview features. It means that, with the latest vscode-java release,preview features can not be enabled for projects compiling against Java 22 (or older), even if you configured the proper runtime. Those projects need to be updated to use Java 23 in vscode-java.

It's recommended you configure thejava.configuration.runtimes preference in your user's settings.json:

"java.configuration.runtimes": [  {"name":"JavaSE-23","path":"/path/to/jdk-23","default":true  },],

Now, depending on the style of Java projects you use, there are different ways to enable preview features.

Standalone Java files

When you open standalone Java files (i.e. which have no Eclipse/Maven/Gradle settings), preview features are enabled by default, without warnings, if vscode-java was started with a JDK 23 orJavaSE-23 is set to be the default injava.configuration.runtimes.

Maven projects

Maven projects need to have the--enable-preview flag added to the maven-compiler-plugin configuration, in their pom.xml:

<projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  <modelVersion>4.0.0</modelVersion>  <groupId>foo.bar</groupId>  <artifactId>demo</artifactId>  <version>0.0.1-SNAPSHOT</version> <properties>  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  </properties>  <build>    <plugins>      <plugin>        <artifactId>maven-compiler-plugin</artifactId>        <version>3.13.0</version>        <configuration>          <release>22</release>          <compilerArgs>--enable-preview</compilerArgs>        </configuration>      </plugin>    </plugins>  </build></project>

Eclipse projects

Eclipse projects need to add the the following preferences to.settings/org.eclipse.jdt.core.prefs:

eclipse.preferences.version=1org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabledorg.eclipse.jdt.core.compiler.codegen.targetPlatform=23org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserveorg.eclipse.jdt.core.compiler.compliance=23org.eclipse.jdt.core.compiler.debug.lineNumber=generateorg.eclipse.jdt.core.compiler.debug.localVariable=generateorg.eclipse.jdt.core.compiler.debug.sourceFile=generateorg.eclipse.jdt.core.compiler.problem.assertIdentifier=errororg.eclipse.jdt.core.compiler.problem.enumIdentifier=errororg.eclipse.jdt.core.compiler.release=enabledorg.eclipse.jdt.core.compiler.source=23org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabledorg.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore

Gradle projects

⚠️Gradle 8.10 minimum is required to build Java 23 projects.

Gradle projects can also maintain the same.settings/org.eclipse.jdt.core.prefs file. Alternatively, adding aneclipse.jdt.file.withProperties hook in build.gradle is possible, but requires the gradlecompileJdt taskto be invoked manually, asBuildship, the underlying Gradle integration tool,doesn't invoke it :

plugins {// Apply the java-library plugin to add support for Java Library    id'java-library'    id'eclipse'}sourceCompatibility=JavaVersion.VERSION_23targetCompatibility=JavaVersion.VERSION_23compileJava {    options.compilerArgs+= ["--enable-preview"]}compileTestJava {    options.compilerArgs+= ["--enable-preview"]}//Buildship doesn't use that hook (https://discuss.gradle.org/t/when-does-buildship-eclipse-customization-run/20781/2)//you need to run `gradle eclipse` separatelyeclipse.jdt.file.withProperties {props->    props['org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures']='enabled'    props['org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures']='ignore'}test {    jvmArgs'--enable-preview'}
Clone this wiki locally

[8]ページ先頭

©2009-2025 Movatter.jp