- Notifications
You must be signed in to change notification settings - Fork496
Enabling Java preview features
Under the hood, instead ofjavac, vscode-java usesECJ, a Java 25 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 24 (or older), even if you configured the proper runtime. Those projects need to be updated to use Java 25 in vscode-java.
It's recommended you configure thejava.configuration.runtimes preference in your user's settings.json:
"java.configuration.runtimes": [ {"name":"JavaSE-25","path":"/path/to/jdk-25","default":true },],
Now, depending on the style of Java projects you use, there are different ways to enable preview features.
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 25 orJavaSE-25 is set to be the default injava.configuration.runtimes.
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>25</release> <compilerArgs>--enable-preview</compilerArgs> </configuration> </plugin> </plugins> </build></project>
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=25org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserveorg.eclipse.jdt.core.compiler.compliance=25org.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=25org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=enabledorg.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignoreGradle 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_25targetCompatibility=JavaVersion.VERSION_25compileJava { 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'}