How to fix Java UnsupportedClassVersionError [duplicate] - java

This question already has answers here:
How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
(51 answers)
Closed 1 year ago.
I've run into this error a few times trying to brush up on my Java. Looked up solutions on the internet and most suggestions were to compile or run the file with a flag/option argument to specify the correct interpreter version. My question is, is there a more permanent solution to this? (im using vscode. Guessing there is something i can add to my settings.json file in the workspace- but what?)
Error: LinkageError occurred while loading main class com.example.demo.DemoApplication
java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication has been compiled by a more recent version of the Java Runtime (class file version 59.65535), this version of the Java Runtime only recognizes class file versions up to 58.0
here is my version details
$ java --version
java 14.0.2 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)
Id like to not have to use flags whenever I compile and run a java file from the command line. Bonus points to you if you can explain why this problem happens in the first place? back when i learned java, you used to have to download and install the JRE and JDK separately. I can understand mistakenly downloading incompatible versions, but now it seems the JRE comes prepackaged with the JDK. How is it possible to have incompatible versions of the compiler and interpreter if this is the case?

This error is telling you that your class was compiled at a higher version of Java than the version you are trying to run with. More specifically, in this case, you compiled your class with Java 15 and try to run it with Java 14.
Depending on your situation, you have two ways to resolve the error: compile your code with an earlier version of Java, or run your code on a newer Java version.
Fix via the Command Line. If you're ready to move entirely to a newer JDK, you should download the newer version and make sure your PATH and JAVA_HOME environment variables are set appropriately. Assuming you have Java 15 JRE, you can run your code with the java command packaged with it.
IntelliJ IDEA. You can also control the version of Java you're using for compiling and running in IntelliJ IDEA. Go to File -> Project Structure… -> Project Settings -> Project and change your Project SDK and Project language level. Next run your project on the newer JRE: go to Run -> Edit Configurations… and change your JRE to 15. Now, when you run your project, it will run with the Java 15 JRE.

java.lang.UnsupportedClassVersionError: com/example/demo/DemoApplication has been compiled by a more recent version of the Java Runtime (class file version 59.65535), this version of the Java Runtime only recognizes class file versions up to 58.0
Set the JDK the same one when you compile and run the project:
That's to say, your file was compiled by JDK15, but the current runtime, which is also installed on your machine is JDK14. The dismatch of build and compile jdk version caused this error.
There're two important configurations to correctly configure your environment and project: java.configuration.runtimes and java.home. The former specifies options for your project's execution environment; the latter specifies your language server's execution environment.
You've installed JDK14, set the above two settings to use it can solve this error.
"java.home": "\PATH TO JDK14\",
"java.configuration.runtimes": [
{
"name": "JavaSE-14",
"path": "\PATH TO JDK14\",
"sources": "\PATH TO JDK14\lib\src.zip",
"javadoc": "https://docs.oracle.com/en/java/javase/14/docs/api",
"default": true
},
],
Cleaning the Java Language Server Worspace and reload the window in VS Code, then run again.
More information about java.configuration.runtimes and java.home, please refer to Configure JDK.

Related

Why I am getting java.lang.UnsupportedClassVersionError while trying to run bazel java target?

I am trying to compile and run the example java project. Instead of using the default JDK I have chosen JDK 15 by providing the setting --java_language_version="15". The complete command looks like this:
bazel run --java_language_version="15" //:ProjectRunner
I can see that it downloads Zulu JDK 15. But running the executable throws the following error:
java.lang.UnsupportedClassVersionError: com/example/ProjectRunner has been compiled by a more recent version of the Java Runtime (class file version 59.0), this version of the Java Runtime only recognizes class file versions up to 55.0
What I already figured out is that it tries to run under JDK/JRE 11 (class file versions up to 55.0) but has been compiled with JDK 15 (class file version 59.0).
What I don't understand is why bazel runs the java application with the default JDK (11).
As you say, the --java_language_version flag only selects what JDK used to compile Java. The JRE used to execute Java may be configured with the --java_runtime_version flag. See https://bazel.build/docs/bazel-and-java#config-jvm.

How to resolve Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0 error? [duplicate]

This question already has answers here:
How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
(51 answers)
Closed last year.
I am trying to run a Spring Boot app in IntelliJ.
But when I try to run I get the following error message:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
myProject
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
Here is the JRE location under my Run Configuration:
I have tried installing a couple of different Java versions here:
Here are my current system / environment variables:
Environment Variables:
JAVA_HOME: C:\Program Files\Java\jdk-11.0.12\bin
System Variables:
Path: C:\Program Files\Java\jdk-11.0.12\bin ....
I tried to find a JRE that matched the version, but haven't been able to find it.
Can someone please tell me what changes I need to make so I can successfully run this application locally?
Open IntelliJ on your project and go to File >> Project Structure >> Project and on the option Project SDK Select java 11 and apply the changes.
Intellij Project structure
Edit--------
Then try these options on File >> Settings.
1.Set Gradle JVM Home
Gradle JVM
2.Set the JDK version in the Project module settings
JDK version in the Project module
3.Check the JDK version in the Modules
version in the Modules
Close IntelliJ and open it again.
If you are trying to execute the code outside your IDE...
The problem is Windows....
Open your command prompt and type "where java". When you do this, you will notice a "weird" path that looks something like this
C:\Program Files\Common Files\Oracle\Java\javapath\java.exe
I can't recall what Windows version introduced this shortcut, but I want to say it was Windows 7. The problem is that this shortcut points to a Runtime Environment that is more recent (newer) than the Java version that was used to compile the code. In other words, Java is backwards compatible; not forward compatible. Compiled code cannot run in a newer Java version.
How do you fix this?
One way is to go to your SYSTEM Environment Variables and edit the Path system variable and add JAVA_HOME at the very beginning of the paths. It will be something like this
%JAVA_HOME%; {YOUR OTHER PATHS HERE}
If you are trying to execute within the IDE, the problem is similar. POTENTIALLY, you can have a conflict between your PROJECT properties and your global IDE properties, where the Runtime Environment points to a newer version of Java than the JDK used to compile. That said...
I have a very weird issue with an application I worked on years ago. It was a client/server application and a coworker of mine was having this issue because, unbeknownst to her, there was version of the server running in the background that was causing the conflict. So, if you have a similar type of application, check your processes using Task Manager and make sure you either 1) shutdown this application and try again, or 2) Uninstall that application and reinstall the new one. In her case, she tried rebooting her PC but that didn't work because the process would start automatically after each reboot.

Android Studio on mac : cant find System compiler

Im getting this on one of my macs when I try to compile even "hello world"
Error:Execution failed for task ':app:compileDebugJava'.
Cannot find System Java Compiler. Ensure that you have installed a JDK (not just a JRE) and configured your JAVA_HOME system variable to point to the according directory.
On my other mac, I dont have any problems. The setup is the same as far as I can tell. It's just that for some reason, on one machine, Android Studio / Gradle pops this error message.
I have set
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
running /usr/libexec/java_home gives:
/Library/Java/JavaVirtualMachines/jdk1.7.0_60.jdk/Contents/Home
running javac -version gives :
javac 1.7.0_60-ea
running java -version gives:
java version "1.7.0_60-ea"
Java(TM) SE Runtime Environment (build 1.7.0_60-ea-b15)
Java HotSpot(TM) 64-Bit Server VM (build 24.60-b09, mixed mode)
In Android Studio I have set file->other settings->default project settings-> JDK location to the JDK location listed above.
I've run out of options, why am I still getting this message?
thanks for any help, Ive wasted hours on this already.
My findings from an identical error proved to be due to a clash of Java Jar files that were not related with the Android studio install.
Specifically the tools.jar and auskey.jar under /Library/Java/Extensions
More info here
Rich_LogicBox solution also worked for me.
I've struggled for weeks with different java versions making no difference.
When I removed the jar files placed in /Library/Java/Extensions by auskey, the Android Studio error disappeared.
One possible solution is :
The android studio needs the jdk 1.6+ by default, but your JDK is 1.7.
You can show the package content of the Android Studioin Applications folder, and the open the info.plist, change the JVM version to your JDK's version, which is 1.7* here.
(you see, my JDK version is 1.8 in my computer)

Intellij IDEA using java7 compiler to compile when I have configured it to use Java6

I have set the Module SDK to 1.6
I have also set the Project SDK to 1.6
and I also change Java compiler options from setting to use java 6
but still it is using java 7 to compile. When I compile or run it gives following error
Using javac 1.7.0_11 to compile java sources
java: Errors occurred while compiling module 'SSLJava6'
Compilation completed with 1 error and 0 warnings in 1 sec
1 error
0 warnings
java: javacTask: source release 1.7 requires target release 1.7
Has anyone faced such problem before? What must be done?
Output of java -version
[aniket#localhost jndiProperties]$ java -version
java version "1.6.0_30"
Java(TM) SE Runtime Environment (build 1.6.0_30-b12)
Java HotSpot(TM) 64-Bit Server VM (build 20.5-b03, mixed mode)
and my JAVA_HOME is also set to same.
Interesting.
Questions:
Check that c:\Users\$user\.IntelliJIdea12\config\options\jdk.table.xml has a reference to JDK6 only. This is what the file looks like for me:If there is a reference to a JDK 7 in there then delete the corresponding <jdk> element and restart Intellij.
I know that you have confirmed the java and javac path. Confirm the javac being used by writing a simple HelloWorld.java in a brand new Intellij project. If in the new project javac6 is used, then it's an issue with the old project configuration.
If there is a public JRE 7 lurking about, try un-installing it temporarily. After un-installation check that there is no cruft left behind in the Windows registry at HKEY_LOCAL_MACHINE\SOFTWARE\JavaSoft\Java Runtime Environment
Check whether the build fails if you rename the JDK7 home dir. If so, try symlinking the JDK7 dir to point to the Java6 installation dir. In the windows world the following will work to do the symlink : mklink /J c:\Java7Home c:\ExistingJava6Home
Try setting your IDEA_JDK_64 environment variable to point to your java SDK you want intelliJ to use for example 'D:\Programs\Java\jdk1.9'.
I encountered this issue when installing the new java 9 early access jdk and trying to run it on intelliJ 15.
This fixed it on IntelliJ 16 EAP versions.

java.lang.UnsupportedClassVersionError [duplicate]

This question already has answers here:
How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version
(51 answers)
Closed 6 years ago.
When I ran a java program I received this error.. Could you please suggest why do we get this error:
Exception in thread "main" java.lang.UnsupportedClassVersionError:
GenerateInvoice (Unsupported major.minor version 49.0)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
This class was compiled with a JDK more recent than the one used for execution.
The easiest is to install a more recent JRE on the computer where you execute the program. If you think you installed a recent one, check the JAVA_HOME and PATH environment variables.
Version 49 is java 1.5. That means the class was compiled with (or for) a JDK which is yet old. You probably tried to execute the class with JDK 1.4. You really should use one more recent (1.6 or 1.7, see java version history).
Another option is to delete all the classes and rebuild. Having build file is an ideal solution to control whole process like compilation, packaging and deployment. You can also specify source/target versions
The code was most likely compiled with a later JDK (without using cross-compilation options) and is being run on an earlier JRE. While upgrading the JRE is one solution, it would be better to use the cross-compilation options to ensure the code will run on whatever JRE is intended as the minimum version for the app.
This was a fresh linux Mint xfce machine
I have been battling this for a about a week. I'm trying to learn Java on Netbeans IDE and so naturally I get the combo file straight from Oracle. Which is a package of the JDK and the Netbeans IDE together in a tar file located here.
located
http://www.oracle.com/technetwork/java/javase/downloads/index.html
file name
JDK 8u25 with NetBeans 8.0.1
after installing them (or so I thought) I would make/compile a simple program like "hello world" and that would spit out a jar file that you would be able to run in a terminal. Keep in mind that the program ran in the Netbeans IDE.
I would end up with this error: java.lang.UnsupportedClassVersionError:
Even though I ran the file from oracle website I still had the old version of the Java runtime which was not compatible to run my jar file which was compiled with the new java runtime.
After messing with stuff that was mostly over my head from setting Paths to editing .bashrc with no remedy.
I came across a solution that was easy enough for even me. I have come across something that auto installs java and configures it on your system and it works with the latest 1.8.*
One of the steps is adding a PPA wasn't sure about this at first but seems ok as it has worked for me
sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
domenic#domenic-AO532h ~ $ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) Server VM (build 25.25-b02, mixed mode)
I think it also configures the browser java as well.
I hope this helps others.

Categories

Resources