exception in thread main java lang unsupportedclassversionerror

На самом деле она расшифровывается как «ваш .jar файл был скомпилирован в более новой версии java, чем та, что используется сейчас». То есть, что бы решить эту проблему , вам нужно установить ту же самую версию java, в которой вы компилировали java код. Ну а если не знаете, какая там версия, то просто попробовать поставить java версии поновее. Например, если команда java -version выдается вам java 1.6, то попробуйте поставить java 1.7.

Но сделать это, например, в Debian Wheezy не так то просто. Но я все подробно описал вот в этой заметке про установку Java на Debian.

Кстати, в дополнение к этой ошибке я еще нашел коды unsupported version. В этой ошибке был код 51.0, а вот какие они бывают и к какой версии java они относятся:

51.0 — Java 7. Так и получается, что нам нужен java 1.7

I installed JDK7, a simple hello word program gets compile but when I run this I got following exception.

I checked java -version on command prompt, it shows Java version 1.4.2_03 but when I tried to install new java version from java.com it says that I’m having recommended Java 7 version.

marked as duplicate by Anders R. Bystrup, Abizern, Dharmendra, shanethehat, Graviton Feb 5 ’13 at 3:17

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

5 Answers 5

Copy the contents of the PATH settings to a notepad and check if the location for the 1.4.2 comes before that of the 7. If so, remove the path to 1.4.2 in the PATH setting and save it.

After saving and applying «Environment Variables» close and reopen the cmd line. In XP the path does no get reflected in already running programs.

Try sudo update-alternatives —config java from the command line to set the version of the JRE you want to use. This should fix it.

Assuming you are using Eclipse, on a MAC you can:

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

1. Introduction

In this short tutorial, we’re going to learn what causes the Java runtime error java.lang.UnsupportedClassVersionError: Unsupported major.minor version and how to fix it.

2. A Look at the Error

Let’s start by looking at an example error:

This error is telling us that our class was compiled at a higher version of Java than the version with which we tried to run it. More specifically, in this case, we compiled our class with Java 11 and tried to run it with Java 8.

2.1. Java Version Numbers

For reference, let’s take a quick look at the Java version numbers. This will come in handy in case we need to download the appropriate Java version.

The major and minor version numbers are stored in the class bytecode at bytes six and seven.

Let’s see how the major version numbers map to Java versions:

  • 45 = Java 1.1
  • 46 = Java 1.2
  • 47 = Java 1.3
  • 48 = Java 1.4
  • 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

3. Fix via the Command Line

Let’s now discuss how we can resolve this error when running Java from the command line.

Depending on our situation, we have two ways we can resolve this error: compile our code for an earlier version of Java, or run our code on a newer Java version.

The final decision depends on our situation. If we need to use a third-party library that’s already been compiled at a higher level, our best option is probably to run our application using a newer Java version. If we’re packaging an application for distribution, it might be best to compile down to an older version.

3.1. JAVA_HOME Environment Variable

Let’s start by checking how our JAVA_HOME variable is set. This will tell us which JDK is being used when we run javac from our command line:

If we’re ready to move entirely to a newer JDK, we can download the newer version and make sure our PATH and JAVA_HOME environment variables are set appropriately.

3.2. Running a New JRE

Returning to our example, let’s look at how we can resolve the error by running it at a higher version of Java. Assuming we have Java 11 JRE in C:Appsjdk-11.0.2, we can run our code with the java command packaged with it:

3.3. Compiling with an Older JDK

If we’re writing an application that we want to be runnable down to a certain version of Java, we need to compile the code for that version.

We can do that in one of three ways: using an older JDK to compile our code, using the -bootclasspath, -source, and -target options of the javac command (JDK 8 and older), or using the –release option (JDK 9 and newer).

Let’s start by using an older JDK, similarly to how we used a newer JRE for running our code:

It’s possible to just use -source and -target, but it might still create class files that aren’t compatible with an older Java.

To ensure compatibility, we can point -bootclasspath at the rt.jar of the targeted JRE:

The above applies mainly to JDK 8 and lower. In JDK 9, the –release parameter was added to replace -source and -target. The –release option supports targets 6, 7, 8, 9, 10, and 11.

Let’s use –release to target Java 8:

Now we can run our code on a Java 8 or higher JRE.

4. Eclipse >

Now that we understand the error and the general approach to correcting it, let’s take what we’ve learned and see how we can apply it when working in the Eclipse IDE.

4.1. Changing the JRE

Assuming we already have Eclipse configured with different versions of Java, let’s change our project’s JRE.

Let’s go to our Project properties, then to Java Build Path, and then the Libraries tab. Once there, we’ll select the JRE and click Edit:

Now, let’s choose Alternate JRE and point to our Java 11 installation:

At this point, our application will run against Java 11.

4.2. Changing the Compiler Level

Let’s now look at how we can change our target to a lower level of Java.

First, let’s go back to our Project properties, then Java Compiler, and check Enable project specific settings:

Here, we can set our project to compile for earlier versions of Java and customize other compliance settings:

5. IntelliJ >

We can also control the version of Java we’re using for compiling and running in IntelliJ IDEA.

5.1. Adding a JDK

Before we do that, we’ll see how to add additional JDKs. Let’s go to File -> Project Structure -> Platform Settings -> SDKs:

Let’s click the plus icon in the middle column, select the JDK from the drop-down, and select our JDK location:

Now, when we run our project, it will run with the Java 11 JRE.

5.3. Changing the Compiler Level

If we’re distributing our application to run on a lower JRE, we need to adjust our compiler level to target the older version of Java.

Let’s go to File -> Project Structure… -> Project Settings -> Project and change our Project SDK and Project language level:

We can now build our project, and the class files generated will run on Java 8 and higher.

6. Maven

When we build and package a file in Maven, we can control the version of Java we target.

When using Java 8 or older, we set the source and target for the compiler plugin.

Let’s set the source and target using compiler plugin properties:

Alternatively, we can set the source and target in the compiler plugin:

With the –release option added in Java 9, we can configure that with Maven as well.

Let’s use a compiler plugin property to set the release:

Or we can configure the compiler plugin directly:

Оцените статью