I am using Java 14 as the default Java platform for Netbeans 11.3 (netbeans_jdkhome is set to my Java 14 JDK) and trying to use a preview feature in a simple Java application. I set the source level to 14 and set --enable-preview as a compiler argument. The code compiles without errors.
However, when I try to run it within Netbeans, it complains that the major version of the .class files is 57 while the runtime only plays well with 58 files and preview features. Here's the error:
java.lang.UnsupportedClassVersionError: javaapplicationtest14/JavaApplicationTest14 (class file version 57.65535) was compiled with preview features that are unsupported. This version of the Java Runtime only recognizes preview features for class file version 58.65535
I checked the major version of the .class files and they are indeed 57. Any ideas why my project won't compile into Java 14 level? I am using an Ant build.
As well as setting --enable-preview as a compiler option, it should also be set as a VM Option when running the code:

However, that doesn't fix the problem, and unfortunately this looks like a NetBeans 11.3 bug. I reproduced your problem with a Java with Ant project, and created Bug Report NETBEANS-4049 UnsupportedClassVersionError when running JDK14 code with --enable-preview.
There are a couple of workarounds if you need to use preview features with JDK 14 in NetBeans:
Update your question with more details if you still have problems.
Optional:
Can use sdkman or set default java path:
/opt/<jdk-install-dir>
C:\Program Files\<jdk-install-dir>


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="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>com.mycompany</groupId>
<artifactId>Demo</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>14</maven.compiler.source>
<maven.compiler.target>14</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<compilerArgs>
<arg>--enable-preview</arg>
</compilerArgs>
</configuration>
</plugin>
</plugins>
</build>
</project>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With