Visual Studio Code retrive UnsupportedClassVersions when run - java

In Visual Studio Code:
I have OpenJDK 14 correctly configured. When I try to run my project I receive the follow error:
java.lang.UnsupportedClassVersionError: (class file version 56.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
This class is a dependency and it is stored in a Nexus Repository, all this project have been compiled with JDK 12. So I try to uninstall OpenJDK 14 to install OpenJDK 12 but If I do this the project can't build because 'Preview features enabled at an invalid source release level 12, preview can be enabled only at source level 14'.
But the same project is correctly build in Eclipse for example.

Can you try running the "Java: clean the Java language server workspace" command?
Or
Updating java extension to the latest version.
As you described, the first time you try to take JRE 14 to run the file which was compiled
with javac 12, then you downgrade to JDK 12 and want to use JRE 12 to run the file. But you get the error of 'Preview features enabled at an invalid source ...'. It seems like a cache causes this. Or did you use some preview features of JDK 14 in your project?

I have solved this problem with following steps:
1 - Install JDK 12 and configure it.
2 - The project was imported from Eclipse so there are all related files in settings. One of this is "org.eclipse.jdt.core.prefs" go into it and modifie the value of "org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures" to disabled.
Seems like VSCode reads all the settings and replicate the
behavior of Eclipse IDE.

Related

SpringBoot has an issue creating bean with MongoDB Java ClassFile Version [duplicate]

While running a script, I am getting the following error message in the Eclipse console:
Class has been compiled by a more recent version of the Java Environment (class file version 53.0), this version of the Java Runtime only recognizes class file versions up to 52.0.
Java Version: 8
IDE: Eclipse Oxygen
Firefox Version: 46
This is just a version mismatch. You have compiled your code using java version 9 and your current JRE is version 8. Try upgrading your JRE to 9.
49 = Java 5
50 = Java 6
51 = Java 7
52 = Java 8
53 = Java 9
54 = Java 10
55 = Java 11
56 = Java 12
57 = Java 13
58 = Java 14
59 = Java 15
60 = Java 16
61 = Java 17
62 = Java 18
63 = Java 19
IDE: Eclipse Oxygen.3
To temporarily correct the problem do the following:
Project menu > Properties > Java Compiler > Compiler compliance level > 1.8
A permanent fix likely involves installing JDK 9.
FYI 1.8 is what Java 8 is called.
Side bar
I recently returned to Java after a foray into C# (a breath of fresh air) and installed Eclipse Oxygen onto a clean system that had never had Java installed on it before. This default everything with a brand new install of Eclipse Oxygen yet somehow or other Eclipse can't get its own parameters to match the jdk that's installed. This is the second project I created and the second time I ran into this headache. Time to go back to C#?
Related Question
has been compiled by a more recent version of the Java Runtime (class file version 53.0)
You might see this error in IntelliJ as well and this comes up in Google.
I think it's a Gradle error more than an IDE error.
The fix (stolen from here) is configure Java 11 as a Gradle JVM in IntelliJ:
File -> Settings -> Build, Execution & Development -> Build Tools -> Gradle and change Gradle JVM to 11
That screenshot stolen from the Jetbrains issue as well
You can try this way
javac --release 8 yourClass.java
For Android Studio, File -> Project Structure [Ctrl + Alt + Shift + S]
53 stands for java-9, so it means that whatever class you have has been compiled with javac-9 and you try to run it with jre-8. Either re-compile that class with javac-8 or use the jre-9
For temporary solution just right click on Project => Properties => Java compiler => over there please select compiler compliance level 1.8 => .class compatibility 1.8 => source compatibility 1.8.
Then your code will start to execute on version 1.8.
I had a similar issue from the console after building a Jar in Intellij. Using the Java configuration to update to a newer version (Windows -> Configure Java -> Update -> Update Now) didn't work and stuck at version 1.8 (Java 8).
To switch to a more recent version locally I had to install the Java 15 JDK from https://www.oracle.com/uk/java/technologies/javase-jdk15-downloads.html and add that to my Java runtime environment settings.
The solution for me was doing what Andrei Kuznetsov said at https://youtrack.jetbrains.com/issue/IDEA-251407/Failed-to-run-Android-project-in-the-Idea-20203-EAP-OutputBuildAction-has-been-compiled-by-a-more-recent-version-of-the-Java, summarized in the following image:
"Root cause: IDEA sources are now compiled against Java 11. Known workaround is to configure Java 11 as a Gradle JVM." (Andrei Kuznetsov)
Your JDK version: Java 8
Your JRE version: Java 9
Here your JRE version is different than the JDK version that's the case. Here you can compile all the java classes using JDK version 1.8. If you want to compile only one java class just change the *.java into <yourclassname>.java
javac -source 1.8 -target 1.8 *.java
source: The version that your source code requires to compile.
target: The oldest JRE version you want to support.
I'm writing this because I found the other answers hard to understand.
Essentially your JRE is not updated and/or Eclipse is not configured to use the most recent JRE.
On Windows, go to Control Panel -> Programs -> Java -> update and proceed to update java
or if you don't have Java, go to Oracle's website and download the most recent JRE.
Once this is done, go into eclipse, and under the project view, right click on your project, select Java Build Path, double click on JRE System Library, then Select Workspace Default JRE 14.1.
If a recent version of Java doesn't show up here, it probably isn't installed. Check you JRE(NOT JDK) version and make sure it's recent. If it is, try restarting the computer then trying this again.
IDE : Eclipse 2021-09
This is caused because you have compiled the code with java version 9 and have java8 installed. You don't have to update to solve this issue.
Right click on project > properties > Java Compiler > untick-Enable
project specific settings > configure workspace settings > tick-Use
default compliance settings > Apply and Close.
Now your code should be executed properly.
I've reproduced on Windows 10 the similar issue in format like:
Error: LinkageError occurred while loading main class ...
java.lang.UnsupportedClassVersionError: com/.../ServiceApplication
has been compiled by a more recent version of the Java Runtime (class file version 62.0),
this version of the Java Runtime only recognizes class file versions up to 61.0
after upgrading the project from Java 17 to Java 18.
I had project settings regarding to Java 18:
Command line:
java version "18.0.1.1" 2022-04-22
Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
Java HotSpot(TM) 64-Bit Server VM (build 18.0.1.1+2-6, mixed mode, sharing)
Project Structure -> Project Settings -> Project:
Project Structure -> Project Settings -> Modules:
Project Structure -> Platform Settings -> SDKs:
And the reason of this issue was:
I forgot to specify the correct version for Run/Debug Configurations -> Build and run to Java 18 instead of Java 17:
As it was compiled with a newer Java version, but running on the older one.
If it's Maven-related issue, you can modify POM by specifying the -source and -target of the Java Compiler.
Additional helpful links:
Setting the Java Version in Maven
Consuming Jar Artifact of a Specific Java Version
Go to Project section, click on properties > then to Java compiler > check compiler compliance level is 1.8 , or there should be no yellow warning at bottom
Refreshing gradle dependencies works for me: Right click over the project -> Gradle -> Refresh Gradle Project.
You should check your Project Facets.
Project Properties > Project Facets
Change java version to the same version you are using. That work for me!
I faced this error in Intellij. Apparently, choosing java 1.8 as the project SDK doesn't affect the javac compiler that's used to build the project.
To change that, go to Settings -> Build, Execution, Deployment -> Compiler -> Java Compiler, and change Project bytecode version to 8.
If you're using IntelliJ IDEA and having this error, try this.
Command + , (Open Preferences)
Build, Execution, Deployment > Build Tools > Gradle
Gradle JVM > Change it to a proper version
This is getting as an example
if your computer running on an older version of java and the Neo4J is compiled on the latest version, please try to install the same version or upper to your computer.
I'm getting the same problem with Sonarqube
has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
Sonarqube with AzureDevOps error output:
INFO: SonarScanner 4.6.2.2472
INFO: Java 1.8.0_231 Oracle Corporation (32-bit)
INFO: Windows Server 2016 10.0 x86
INFO: User cache: C:\Windows\system32\config\systemprofile.sonar\cache
INFO: Scanner configuration file: C:\agent_a_work_tasks\SonarQubePrepare_15b84ca1-b62f-4a2a-a403-89b77a063157\5.4.0\classic-sonar-scanner-msbuild\sonar-scanner-4.6.2.2472\bin..\conf\sonar-scanner.properties
INFO: Project root configuration file: C:\agent_a_work\189.sonarqube\out\sonar-project.properties
INFO: Analyzing on SonarQube server 9.4.0
INFO: Default locale: "en_AU", source code encoding: "windows-1252" (analysis is platform dependent)
INFO: ------------------------------------------------------------------------
INFO: EXECUTION FAILURE
INFO: ------------------------------------------------------------------------
INFO: Total time: 0.405s
INFO: Final Memory: 3M/15M
##[error]ERROR: Error during SonarScanner execution
java.lang.UnsupportedClassVersionError: org/sonar/batch/bootstrapper/EnvironmentInformation has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
I had Java v11 installed on the Sonarcube server, but the error is saying its the Build Agent that doesn't have Java v11.
I decided to add the Java v12 bin path to the Environment Variable Path for it to be detected. IMPORTANT THIS NEEDS TO BE DONE ON THE BUILD AGENT:
For a Hosted Build Agent you can install JDK v11 or use this PowerShell script step:
- task: PowerShell#2
displayName: 'Download and Install Java v11'
inputs:
targetType: 'inline'
script: |
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$Command = "c:\jdk-11.0.15_windows-x64_bin.exe"
$Parms = "/s"
Write-Host [System.IO.File]::Exists($Command)
if ([System.IO.File]::Exists($Command)) {
Write-Host 'Downloading Java11.exe'
Invoke-WebRequest https://yourOwnUrl.com/jdk-11.0.15_windows-x64_bin.exe -OutFile $Command
}
Write-Host [System.IO.File]::Exists($Command)
& "$Command" $Prms
[System.Environment]::SetEnvironmentVariable("JAVA_HOME", "C:\Program Files\Java\jdk-11.0.15")
[System.Environment]::SetEnvironmentVariable("Path", [System.Environment]::GetEnvironmentVariable('Path', [System.EnvironmentVariableTarget]::Machine) + ";$($env:JAVA_HOME)\bin")
In my case, I'm building with VS2019 some C++ code that is using ANTLR4.
ANTLR4 is java based, and to use it I run java commands as a custom build step.
Earlier that day I had updated my JRE (Java Runtime Environment) on my new laptop to 1.8. Not thinking, since it's been a year since I ran ANTLR4 stuff... I need the full JDK to COMPILE with java. So I had a 1.8 RUNTIME, but the VS2019 build shell was finding the JDK1.7 compiler. Once I installed the 1.8 JDK, all was good.
So 80% of the time JRE is fine for me... it's just when I need the full JDK, DOH I need the full JDK.
In build.gradle(app) update as below: (Worked for me)
kotlinOptions {
jvmTarget = '1.8'
}
For me this problem was resolved by just installing older version of testng. Looks like It compiles with my Java 8
If you're using IntelliJ IDEA and having this error, try this.
1. Command + , (Open Preferences)
2. Build, Execution, Deployment > Build Tools > Gradle
3. Gradle JVM > Change it to a proper version
Check the version of Java configured in IDE by navigating to Settings -> Build, Execution, Deployment > Build Tools > Gradle
In my case, I use the IntellJ Idea and set JDK11 as my SDK and JDK17 in gradle setting.
Just update my SDK as JDK17, it was solved
Check the JDK in "Project Setting"(CMD+;) -> SDK
with
the gradle JVM in "Preference"(CMD+,) -> Build Tools -> gradle JVM
Faced the same issue when initialise my project from spring initizer
We need to match the version of java.sourceCompatibility and jvmTarget used for run and compile in "build.gradle.kt" file.
Last week, I updated my Mac's Android Studio from 2021.2.1 to 2021.3.1. Then our project was built failed for the similar reason:
Cause: com/android/tools/idea/gradle/run/OutputBuildAction has been
compiled by a more recent version of the Java Runtime (class file
version 55.0), this version of the Java Runtime only recognizes class
file versions up to 53.0
I found several discussion on the Jetbrains YouTrack Forum: Failed to run Android project in the Idea 2020.3 EAP (OutputBuildAction has been compiled by a more recent version of the Java Runtime (class file version 55.0)). The suggested workaround is to update the defined Gradle JDK version to JDK11, but many Android projects don't support Gradle JDK11, so does mine.
Another solution is to downgrade Android Studio from 2021.3.1 to 2021.2.1, and it worked for me. Hope it helps.
it looks like everyone is using windows for development, but if you use Linux, lower your Java version. maybe install JDK 8, the version table provided by #Nithin should give you an idea of which version you want to install.
After configuring the version of the compiler to 1.8 (your required version), try the older version of spring boot currently it is 3.0.0 by default but you can try 2.7.6 instead.
enter image description here

OpenCV build from source fails with Java 11: warning: target release 11 conflicts with default source release 18

I’m working on a Java application that runs Java 11. I’ve chosen Java 11 because it’s the the last Java version with LTS.
I’m using OpenCV in this Java application and am using libraries from the contrib modules. I’m using Homebrew to install OpenCV with the Java bindings and the contrib modules.
The first thing I tried was to install OpenCV from source without specifying the Java target version. When I did this, OpenCV installed correctly, but when I tried to use it in code, I got the following error:
class file has wrong version 61.0, should be 55.0
I take this to mean that because OpenCV was compiled via Java 17, I can’t use it in my Java 11 code. It’s also worth mentioning that because I’m using Maven with my Java project, I first export the OpenCV JAR into a local Maven module via something like mvn install:install-file -Dfile=/usr/local/Cellar/opencv/4.5.5_1/share/java/opencv4/opencv-455.jar. I’m using OpenJDK 11 as my JRE for this export.
I’m also using OpenJDK 11 as my default JDK on my Mac (java -version and javac -version both return OpenJDK 11). So I was a bit surprised to see this error.
So to solve it, I’ve been trying to install OpenCV with the flag -DOPENCV_JAVA_TARGET_VERSION=11. However, when I do this, I get the following error:
[ 11%] Generating opencv-455.jar
cd /tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv && /usr/local/bin/ant -noinput -k jar
Picked up _JAVA_OPTIONS: -Duser.home=/Users/dpapp/Library/Caches/Homebrew/java_cache
Buildfile: /private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build.xml
[tstamp] Honouring environment variable SOURCE_DATE_EPOCH which has been set to 1640404407
jar:
[javac] Compiling 280 source files to /private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build/classes
[javac] warning: target release 11 conflicts with default source release 18
Target 'jar' failed with message 'Compile failed; see the compiler error output for details.'.
BUILD FAILED
/private/tmp/opencv-20220410-81173-p3o3uo/opencv-4.5.5/build/modules/java/jar/opencv/build.xml:14: Compile failed; see the compiler error output for details
This error makes it sound like OpenCV requires at least JDK 18, which doesn’t sound right to me. Is there a minimum JDK for each OpenCV version? Should I be using OpenCV 3 instead? Any help is greatly appreciated, thank you!
I partially figured out what's going on here.
As a prereq for running brew install opencv, I ran brew install ant. brew install ant installs the latest OpenJDK (OpenJDK 18 at the time of writing) and ignores the Java home path set on my machine. This causes brew install opencv to try to install OpenCV with Java 18 while the target is set to 11, which throws an error.

Apache NetBeans IDE 12.0 cannot create new java application

I installed Java NetBeans IDE 12.0 and JDk-16. When I try to create a project using Java With Ant, it get stuck like in the screenshot. When I cancel it, it creates the file but no code in it i.e., no main method is created. Just a blank file.
Does anyone know how to solve this?
From the NetBeans documentation for "Deployment Platforms" for NetBeans 12.0:
Apache NetBeans 12.0 runs on the JDK LTS releases 8 and 11, as well as
on JDK 14, i.e., the current JDK release at the time of this NetBeans
release.
NetBeans may work (or appear to work) with newer JDK releases that are not officially supported such as JDK 16, but you run the risk of hitting unexpected problems.
So if you don't need any Java features released after JDK 14, download and install JDK 14, and add it as a Java platform in NetBeans, using Tools > Java Platforms > Add Platform.... You don't need to uninstall JDK 16.
However, if you really need JDK 16 features then use a different IDE. The latest release of Eclipse, 2021-03, explicitly supports JDK 16.
I had the same problem and was able to solve it in windows by following the steps below:
Uninstall Netbeans 12.4. Make sure to delete all the cache files
(Drive:\users\user\AppData\Roaming\Netbeans and
Drive:\users\user\AppData\Local\Netbeans) (Windows)
Check your JDK 16 directory (no JRE) and environment variables JAVA_HOME, CLASSPATH and PATH.
Install Netbeans 12.4
Create new project and check that you select Download and activate nb-javac.
For me it helped using NetBeans version 12.5 and the JDK 14. After that everything was running smooth.
I had the same problem and upgrading to version 12.5 worked for me.

Eclipse oxygen gives syntax error warning in module-info file

I am trying to learn new concept of modules in JAVA. I am using Eclipse oxygen 4.7.3a with JDK 11.0.1.
I am getting syntax error warning on my module-info.java file. It seems that compiler is not able to parse new keywords of module-info.java file.
I performed following solution to fix this issue :
Installed all JAVA 11 updates from Eclipse JAVA 11 updates URL : http://download.eclipse.org/eclipse/updates/4.9-P-builds
Added JVM argument "--add-modules=java.se.ee" to eclipse.ini file.
But issue is still there . Any suggestions/inputs appreciated !
Java 11 requires
Eclipse 2018-09 (4.9) and
the Java 11 Support for Eclipse 2018-09 (4.9) plugin
or Eclipse 2018-12 (4.10) which will be released on December 19, 2018.
Eclipse Oxygen was released before Eclipse Photon, Eclipse 2018-09 and Java 11 and therefore cannot detect a Java 11 JDK/JRE yet.
In addition, the Java compiler compliance level must set to 9 or higher (Project > Properties: Java Compiler) to have a module-info.java file with its own syntax which has been introduced with the Java Platform Module System (JPMS) in Java 9. If the compiler compliance level is lower than 9 the token module at the beginning of a .java file is a syntax error because module-info.java is a regular Java file in Java <= 8.
Please note that the setting in eclipse.ini refers to the JRE/JDK that is used to run Eclipse. The JREs or better (to see the source code) JDKs to use for development have to be specified in Window > Preferences: Java > Installed JREs.

Trouble setting up JDK in eclipse

COMPANY-0034:Home deter_dangler$ pwd
/Library/Java/JavaVirtualMachines/jdk-9.0.4.jdk/Contents/Home
COMPANY-0034:Home deter_dangler$ ls
README.html conf jmods lib
bin include legal release
COMPANY-0034:Home deter_dangler$
Terminal shows I have JDK 9.0 installed and I configured JAVA SE 9.0 in eclipse as below:
But when I am trying configure build path for any Java project as below, it shows Native Library Location as 'None' and shows errors in my .Java file:
What is missing?
EDIT:
Version: Kepler Service Release 1
Build id: 20130919-0819
According to Eclipse's website, (here), Java 9 is only officially supported on Eclipse Oxygen.
If you can, I recommend upgrading to Oxygen so you can avoid compatibility issues.

Categories

Resources