Вопрос: Как исправить java.lang.UnsupportedClassVersionError: Неподдерживаемая версия major.minor


Я пытаюсь использовать Notepad ++ как мой инструмент «все-в-одном» редактировать, запускать, компилировать и т. д.

у меня есть JRE установлен, и я установил свою переменную пути в каталог ... / bin.

Когда я запускаю свой «Hello world» в Notepad ++, я получаю это сообщение:

java.lang.UnsupportedClassVersionError: test_hello_world :
 Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClassCond(Unknown Source)
       .........................................

Я думаю, проблема здесь в версиях; некоторые версии Java могут быть старыми или слишком новыми.

  1. Как это исправить?
  2. Должен ли я установить JDK и настроить свою переменную пути в JDK вместо JRE?
  3. В чем разница между PATHпеременная в JRE или JDK?

1375


источник


Ответы:


Показанный номер версии описывает версию JRE, с которой совместим файл класса.

Основные цифры:

Java SE 10 = 54,
Java SE 9 = 53,
Java SE 8 = 52,
Java SE 7 = 51,
Java SE 6.0 = 50,
Java SE 5.0 = 49,
JDK 1.4 = 48,
JDK 1.3 = 47,
JDK 1.2 = 46,
JDK 1.1 = 45

(Источник: Википедия )

Чтобы исправить настоящую проблему, попробуйте либо запустить Java-код с более новой версией Java JRE, либо указать целевой параметр компилятору Java, чтобы дать команду компилятору создать код, совместимый с более ранними версиями Java.

Например, чтобы генерировать файлы классов, совместимые с Java 1.4, используйте следующую командную строку:

javac -target 1.4 HelloWorld.java

С более новыми версиями компилятора Java вы, скорее всего, получите предупреждение о том, что путь класса bootstrap не задан. Более подробную информацию об этой ошибке можно найти в блоге Новое предупреждение javac для установки более старого источника без bootclasspath ,


1765



java.lang.UnsupportedClassVersionErrorпроисходит из-за более высокого JDK во время компиляции и более низкого JDK во время выполнения.


328



В Eclipse я просто пошел в команду меню Окно -> предпочтения -> Ява -> составитель а затем установите «Уровень соответствия компилятора» на 1.6.


110



Не волнуйся, я решил.

Это на самом деле просто - вам нужно установить BOTH JRE / JDK с той же версией.

JRE 6 -> JDK 6

JRE 7 -> JDK 7

И так далее.


55



This error means you're trying to load a Java "class" file that was compiled with a newer version of Java than you have installed.

For example, your .class file could have been compiled for JDK 7, and you're trying to run it with JDK 6.

So the solution is to either:

  • Upgrade your Java runtime or
  • Recompile the class if you have the source, using your local Java compiler (if you have one).

    javac FileName.java

For developers, this can happen if another developer checks in a .class file, and they've got a newer version of java than you have!


37



You are trying to run your program with a Java version that does not support the version in which the code was compiled. So basically you must have compiled your code with a higher version and trying to run it using a lower version.

As you are getting

Unsupported major.minor version 51.0

and version 51.0 corresponds to J2SE 7 you have most probably compiled your code in Java 7 and trying to run it using a lower version. Check what java -version displays. It should be the Java 7 version. If not make appropriate changes in the PATH/JAVA_HOME. Or you can compile with the same version you are trying to run the code. If the configurations are confusing you can always give absolute path /home/user/jdk1.7.0_11/bin/javac and /home/user/jdk1.7.0_11/bin/java.


20



I had a similar situation on Mac, and the following process worked for me:

In the terminal, type

vi ~/.profile

Then add this line in the file, and save

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk<version>.jdk/Contents/Home

where version is the one on your computer, such as 1.7.0_25.

Exit the editor, then type the following command make it become effective

source ~/.profile 

Then type java -version to check the result

java -version 

What is .profile file?

.profile file is a hidden file. It is an optional file which tells the system which commands to run when the user whose profile file it is logs in. For example, if my username is bruno and there is a .profile file in /Users/bruno/, all of its contents will be executed during the log-in procedure.

Source: http://computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515


17



In Eclipse's menu Window -> Preferences -> Java -> Compiler check also "Configure Project Specific Settings".

If you stil have the error with same Java version: try to delete build folder of your project manually. Then restart Eclipse.


15



You can have some JAR library compiled in Java 7, and you have only Java 6 as Java Runtime. It could happen with some new libraries.


13



The most common issue is misconfiguration of your JAVA_HOME variable which should point to the right Java Development Kit library, if you've multiple installed.

To find where SDK Java folder is located, run the following commands:

jrunscript -e 'java.lang.System.out.println(java.lang.System.getProperty("java.home"));'

Debian/Ubuntu

To check which java (openjdk) you've installed, check via:

dpkg -l "openjdk*" | grep ^i

or:

update-java-alternatives -l

To change it, use:

update-alternatives --config java

Prefix with sudo if required.

to select the alternative java version.

Or check which are available for install:

apt-cache search ^openjdk

Prefix with sudo if required.

Then you can install, for example:

apt-get install openjdk-7-jre

Prefix with sudo if required.

Fedora, Oracle Linux, Red Hat

Install/upgrade appropriate package via:

yum install java-1.7.0-openjdk java-1.7.0-openjdk-devel

The java-1.7.0-openjdk package contains just the Java Runtime Environment. If you want to develop Java programs then install the java-1.7.0-openjdk-devel package.

BSD

There is an OpenJDK 7 package in the FreeBSD Ports collection called openjdk7 which probably needs to be reconfigured.

See: OpenJDK wiki page.

Windows

Just install appropriate Java SE Development Kit library from the Oracle site or install

Jenkins

If you're experiencing this issue with Jenkins, see:

However selecting the right version of Java (newer) with update-alternatives should work.


13



I have faced the same problem when I was working with an Ant script to build my application.

I use Eclipse for my application development, and I changed the compiler version in build properties of the project. But that didn't work for me. Then I found out that I can provide the compiler version in the Ant script.

I modified the Ant script at the section where it compile Java files.

<target name="build-java" depends="prepare-build">
    <echo message="Compiling java files"/>
    <javac ....
           target="1.5"...
    </javac>
</target>

This worked for me to resolve the unsupported major minor issue.


11



When I installed JDK 1.7, the problem got solved.


11