如何修复java.lang.unsupportedClassversionError:不支持的Major.Minor版本
我正在尝试使用 Notepad ++ 作为我的全in-in-in-in-ine工具,运行,运行,编译等
。 .../bin
目录。
当我在记事本++中运行“ Hello World”时,我会收到此消息:
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的最新版本会产生更详细的异常消息:
java.lang.unsupportedclassversionError:com/xxx/yyy/application已由Java Runtime的最新版本(类文件版本65.0)编辑,此版本的Java Runtime仅识别最多61.0 <<61.0 <<61.0 < /p>
我认为问题 最高这是关于版本的; Java的某些版本可能是旧的或太新的。
- 我该如何修复?
- 我应该安装JDK,并将路径变量设置为JDK而不是JRE吗?
- JRE或JDK中的
PATH
变量有什么区别?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(30)
就我而言,问题是在服务器运行时配置中:
nofollow noreferrer”>
该项目在版本1.7中,服务器JRE设置为1.6 ,在更改了适当的Java版本后,它的发布正常。
In my case the problem was in the server runtime configuration:
Check the JRE is the version you need:
The project was in version 1.7 and the server JRE was set as 1.6, after changing to the proper java version it's launched fine.
我遇到了一个问题,我必须从命令行上进行项目汇编以运行单位测试。如果我对测试类进行了更改,并让Eclipse自动重新编译,那么我会得到“不支持的Major.Minor版本51.0”错误。
我确实已经安装了JDK6和JDK7,但是我所有的JRE设置都指向1.6,无论是在POM和Eclipse的Project Properties页面上。没有数量的Maven更新项目和/或刷新解决此问题。
最终,我尝试关闭项目并重新开放,这似乎可以解决! Hth
I had the problem whereby I was having to run a Maven compilation on my project from the command line in order to run my unit tests; if I made a change to the test class and let Eclipse automatically recompile it, then I got the "Unsupported major.minor version 51.0" error.
I do have both JDK6 and JDK7 installed, but all my JRE settings were pointing at 1.6, both in the pom and from the project properties page in Eclipse. No amount of Maven Update Project and/or refreshing solved this.
Finally I tried closing the project and re-opening it, and this seemed to fix it! HTH
您已经使用JDK 7编译了Java类,并且您正在尝试在JDK 6上运行同一班。
You have compiled your Java class with JDK 7 and you are trying to run same class on JDK 6 .
显示的版本编号描述了类文件与JRE的版本兼容。
数字是:(
来源: wikipedia )
报告的主要 要么使用较新版本的Java JRE运行Java代码,要么将目标参数指定到Java编译器,以指示编译器创建与早期Java版本兼容的代码。
例如,为了生成与Java 1.4兼容的类文件,请使用以下命令行:
使用Java编译器的较新版本,您可能会在未设置的Bootstrap类路径上发出警告。有关此错误的更多信息,请在博客文章 新的Javac警告中,无需设置较旧的源即可BootClassPath 。
The version number shown describes the version of the JRE the class file is compatible with.
The reported major numbers are:
(Source: Wikipedia)
To fix the actual problem you should try to either run the Java code with a newer version of Java JRE or specify the target parameter to the Java compiler to instruct the compiler to create code compatible with earlier Java versions.
For example, in order to generate class files compatible with Java 1.4, use the following command line:
With newer versions of the Java compiler you are likely to get a warning about the bootstrap class path not being set. More information about this error is available in a blog post New javac warning for setting an older source without bootclasspath.
java.lang.unsupportedclassversionError
发生在编译时间内的JDK较高,并且在运行时降低JDK。java.lang.UnsupportedClassVersionError
happens because of a higher JDK during compile time and lower JDK during runtime.在Eclipse中,我只是去菜单命令 window - &gt; 首选项 - &gt; java - &gt; 编译器,然后将“编译器合规级”设置为1.6。
In Eclipse, I just went to menu command Window -> Preferences -> Java -> Compiler and then set "Compiler compliance level" to 1.6.
不用担心,我解决了问题。
它实际上很简单 - 您需要同时安装JRE / JDK具有相同版本。
JRE 6-&GT; JDK 6
JRE 7-&GT; JDK 7
等。
Don't worry, I got it solved.
It is actually simple - you need to install BOTH JRE / JDK with the same version.
JRE 6 -> JDK 6
JRE 7 -> JDK 7
And so on.
此错误意味着您正在尝试加载一个与安装的Java更新版本的Java“类”文件。
例如,您的
.class
文件本可以为JDK 7编译,并且您正在尝试使用JDK 6运行它。因此,解决方案是:
recompile如果您有源,则使用本地Java编译器(如果有)。
javac filename.java
对于开发人员,如果其他开发人员在.class文件中检查,并且它们的Java版本比您拥有的更新!
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:
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!
您正在尝试使用不支持编译代码的版本的Java版本运行程序。 因此,基本上您必须使用更高版本编译了代码,并尝试使用较低版本来运行它。
在获得
和版本 51.0 对应于 J2SE 7 您很可能已经在Java 7中编译了代码,并尝试使用较低版本来运行它。检查什么
Java -version
显示。它应该是Java 7版本。如果没有在路径/Java_home中进行适当的更改。或者,您可以使用试图运行代码的相同版本进行编译。如果配置令人困惑,您始终可以给出绝对路径/home/user/jdk1.7.0_11/bin/javac
and/home/USER/JDK1.7.0_11/bin/java 。
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
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
.我在Mac上也有类似的情况,以下过程对我有用:
在终端中,
然后在文件中添加此行,然后保存
在计算机上的版本中,例如
1.7.0_25 。
退出编辑器,然后键入以下命令使其有效
,然后键入Java -version以检查结果
是什么是
.profile
文件?来源: http:// computers .tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515
I had a similar situation on Mac, and the following process worked for me:
In the terminal, type
Then add this line in the file, and save
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
Then type java -version to check the result
What is
.profile
file?Source: http://computers.tutsplus.com/tutorials/speed-up-your-terminal-workflow-with-command-aliases-and-profile--mac-30515
在Eclipse的菜单窗口 - &gt; 首选项 - &gt; java - &gt; 编译器还检查“配置项目特定设置”。
如果您仍然有相同Java版本的错误:尝试手动删除项目的构建文件夹。然后重新开始日食。
In Eclipse's menu Window -> Preferences -> Java -> Compiler check also "Configure Project Specific Settings".
If you still have the error with same Java version: try to delete build folder of your project manually. Then restart Eclipse.
您可以在Java 7中编译一些JAR库,并且只有Java 6作为Java运行时。一些新图书馆可能会发生。
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.
最常见的问题是您的
Java_home
变量的配置错误,如果您已安装了多个,则应指向正确的Java开发套件库。要查找SDK Java文件夹的位置,请运行以下命令:
Debian/ubuntu
检查已安装的Java(OpenJDK),请通过:
或:
要更改它,请使用:
prefix,
sudo如果需要。
选择替代Java版本。
或检查可用于安装的哪些:
sudo 如果需要。
前缀
fedora,oracle linux,红色帽子
安装/升级适当的软件包通过:
BSD
在FreeBSD端口集合中有一个openJDK 7包,称为 openjdk7 。
请参阅: openjdk wiki page 。
Windows
只需从或安装
Jenkins
,如果您正在与Jenkins遇到此问题,请参见:
但是,使用
update -anternatives
选择正确的Java(较新)应该有效。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:
Debian/Ubuntu
To check which java (openjdk) you've installed, check via:
or:
To change it, use:
Prefix with
sudo
if required.to select the alternative java version.
Or check which are available for install:
Prefix with
sudo
if required.Then you can install, for example:
Prefix with
sudo
if required.Fedora, Oracle Linux, Red Hat
Install/upgrade appropriate package via:
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.我在1.7中编写的项目遇到了同样的问题,并试图以1.6的速度执行。
我在Eclipse中的解决方案:
右键单击您的项目属性 - &gt; Java构建路径 - &GT;库
选择您的JRE系统库,然后在右侧单击编辑,然后选择目标JRE。
现在转到左侧的 Java编译器,然后将编译器合规级别更改为目标。
那对我有用。
I got the same problem with a project written in 1.7 and tried to execute in 1.6.
My solution in Eclipse:
Right click on your Project Properties -> Java Build Path -> Libraries
Select your JRE System Library and click Edit on the right, and choose the target JRE.
Now go to Java Compiler on the left, and change the Compiler compliance level to your target.
That worked for me.
当我使用 ant 脚本以构建我的应用程序时,我也遇到了同样的问题。
我使用 eclipse> eclipse 用于我的应用程序开发,我更改了构建属性中的编译器版本项目。但这对我不起作用。然后,我发现我可以在ANT脚本中提供编译器版本。
我在编译Java文件的部分上修改了ANT脚本。
这为我解决了不支持的主要小问题。
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.
This worked for me to resolve the unsupported major minor issue.
当我安装 jdk 1.7时,问题就解决了。
When I installed JDK 1.7, the problem got solved.
基于此...
在Eclipse中,右键单击“软件包资源管理器”中的项目:
构建路径 - &gt; 配置构建路径
下:
java构建路径 - &gt; 库 - &gt; 添加库 - &gt; JRE系统库 - &gt; 安装JRES - &gt; 搜索。
通过在搜索完成后选择可用的列表中的库来添加所需的JRE。
Based on this...
In Eclipse, right click on project in package explorer:
Build Path -> Configure Build Path
Under:
Java Build Path -> Libraries -> Add Library -> JRE System Library -> Installed JREs -> Search.
Add the required JRE by selecting the library in the list available after the search is complete.
正如几个人在其他地方回答的那样,Java程序正在使用较旧版本的Java进行,而不是被编制的Java。它需要“交叉”才能向后兼容。换句话说,源和目标Java版本之间存在不匹配。
更改Eclipse菜单中的选择不会回答原始海报,后者说他/她不使用Eclipse。在OpenJDK Javac版本1.7上,如果使用参数
-source
和-target
,则可以对1.6进行交叉串联,并提供 rt.jar -file在编译时(即较旧的)目标版本。如果您实际安装了1.6 JRE,则可以指向其安装(例如,/usr/lib/jvm/java-6-6-openjdk-i386/jre/lib/lib/rt.jar在ubuntu,/usr/usr/jdk/jdk1上。 6.0_60/jre/lib/rt.jar显然很抱歉,我不知道它在Windows系统上的位置)。像这样:看起来您可以从Internet下载RT.Jar,然后指向它。但是,这不是太优雅:
As answered elsewhere by several people, the Java program is being run on an older version of Java than the one it was compiled it for. It needs to be "crosscompiled" for backward compatibility. To put it another way, there is a mismatch between source and target Java versions.
Changing options in Eclipse menus don't answer the original poster, who said he/she is not using Eclipse. On OpenJDK javac version 1.7, you can crosscompile for 1.6 if you use parameters
-source
and-target
, plus provide the rt.jar -file of the target version (that is, the older one) at compile time. If you actually install the 1.6 JRE, you can point to its installation (for example, /usr/lib/jvm/java-6-openjdk-i386/jre/lib/rt.jar on Ubuntu, /usr/jdk/jdk1.6.0_60/jre/lib/rt.jar on SunOS apparently. Sorry, I don't know where it is on a Windows system). Like so:It looks like you can just download rt.jar from the Internet, and point to it. This is not too elegant though:
如果使用Maven,请设置Java编译级别。打开命令行,并为您的编译级别写
Java -version
:如果您使用Intellij Idea,请选择Project→ file> file →< em>设置→构建执行部署→编译器→ java编译器。然后更改字节代码为1.7,如此图像:
If you use Maven, set your Java compile level. Open a command line and write
java -version
for your compile level:If you use IntelliJ IDEA, select project → File → Settings → Build Execution Deployment → Compiler → Java Compiler. Then change byte code as 1.7 like this image:
如果您在使用 >。
更新:设置
source
和目标
to1.8
,如果您使用的是JDK 8。If you're facing this issue while using Maven, you can compile your code using the plug-in Maven Compiler.
UPDATE: set
source
andtarget
to1.8
, if you are using JDK 8.从Eclipse运行ANT时,我有相同的错误消息,但是此处提到的其他解决方案并没有解决我的问题。有趣的是,从Windows命令行中运行蚂蚁运行正常,因此必须是Eclipse中的配置问题。
事实证明,在日食下,您可以指定蚂蚁应与之运行的环境,并将其设置为JRE而不是JDK。
I had the same error message when running Ant from Eclipse, but the other solutions mentioned here didn't solve my problem. The funny thing was that running Ant from the Windows command line was running fine, so it had to be a configuration issue within Eclipse.
It turned out that under Eclipse you can specify the environment that Ant should be running with and this was set as a JRE instead of a JDK.
您已经使用了更高版本的JDK编译并尝试从较低版本的JDK/。
要检查此信息,请参阅版本信息:
它们将不同,Javac将具有更高的版本号。
为了解决这个问题,请使用JDK版本中的Java运行,或者如果您有较新的JRE/JDK,也可以使用。
Javac
将告诉您位置,例如/usr/bin/javac
。只需使用/usr/bin/java&lt; program&gt;
直接运行。或者,您可以将环境变量设置为永久解决方案。
You have used a higher version of the JDK to compile and trying to run from a lower version of JDK/JRE.
To check this, see the version information:
They will be different and javac will have a higher version number.
To get around this, run using java from the JDK version or if you have a newer JRE/JDK that will work as well.
which javac
will tell you the location, for example,/usr/bin/javac
. Just run directly using/usr/bin/java <program>
.OR you can set the environment variable as a permanent solution.
此错误意味着用于执行类代码的JRE无法识别使用的Java版本。通常是因为生成类文件的Java版本(即编译)更新。
要修复它,您可以
a)使用将其用于运行它的Java编译器的Java编译器的Java源编译。即安装适当的JDK。
b)使用Java编译器的较新版本,但以兼容模式编译Java源。 IE使用
-target
参数。c)在JRE中运行与JDK编译类别相同的JRE或更新版本。
您可以检查当前正在使用的版本
javac -version
编译器,java -version
用于运行时。对于汇编,当然,请安装和配置您想要的特定JDK。
对于运行时,您可以使用JDK或独立JRE随附的一个,但是无论如何,请确保您已经安装了正确的版本,并且您已经配置了路径,以免感到惊讶。
路径环境变量告诉命令外壳在哪里查找您键入的命令。当您键入
Java
时,命令Shell解释器将浏览PATH
变量(从左到右)中指定的所有位置,以找到适当的Java 运行时可执行。如果已安装了多个版本的Java版本 - 即,在路径变量中指定的多个位置可执行
java
,则在从左到右的情况下遇到的第一个将是执行的一个。编译器命令是
javac
,仅包含JDK。运行时命令为java
,并带有JDK,并且在JRE中。已安装了
Javac
的一个版本(51.0 = Java 7),并且安装了相同的版本Java
已安装,但是另一个先前版本的<代码> Java 在路径上出现,因此被调用而不是您期望的。This error means that the JRE that is being used to execute your class code does not recognise the version of Java used. Usually because the version of Java that generated your class file (i.e. compiled it) is newer.
To fix it, you can either
a) Compile your Java sources with the same, or older, version of the Java compiler as will be used to run it. i.e. install the appropriate JDK.
b) Compile your Java sources with the newer version of the Java compiler but in compatibility mode. i.e. use the
-target
parameter.c) Run your compiled classes in a JRE that is the same, or newer, version as the JDK used to compile the classes.
You can check the versions you are currently using with
javac -version
for the compiler, andjava -version
for the runtime.For compilation, certainly, install and configure the specific JDK that you want.
For runtime, you can use the one that comes with the JDK or a standalone JRE, but regardless, make sure that you have installed the right versions and that you have configured your PATH such that there are no surprises.
The PATH environment variable tells the command shell where to look for the command you type. When you type
java
, the command shell interpreter will look through all the locations specified in thePATH
variable, from left to right, to find the appropriatejava
runtime executable to run. If you have multiple versions of Java installed - i.e. you have thejava
executable in multiple locations specified in the PATH variable, then the first one encountered when going from left to right will be the one that is executed.The compiler command is
javac
and only comes with the JDK. The runtime command isjava
and comes with the JDK and is in the JRE.It is likely that you have one version (51.0 = Java 7) of
javac
installed, and you also have the same version ofjava
installed, but that another previous version ofjava
is appearing earlier in the PATH and so is being invoked instead of the one you expect.当我恢复到Java 6并试图与Java 7一起运行课程时,有这个问题。对我有用的是Prevenenes&gt; Java&gt;编译器 - &GT;将合规级设置为1.6,并至关重要的是“配置项目设置”。
Had this problem when I reverted to Java 6 and tried to run classes previously compiled with Java 7. What worked for me was Preferences > java > compiler --> set compliance level to 1.6 and crucially "configure project settings"..
今天,此错误消息出现在我们的
) >撑杆用Java 7编译了应用程序。
事实证明,有人使用“服务tomcat [stop/start]”重新启动tomcat 7,
会导致“不支持的Major.Minor版本51.0”错误。
当我们使用“ /etc/init.d/tomcat7 [停止/启动]”重新启动Tomcat 7时,解决了问题。
Today, this error message appeared in our Tomcat 7 on Ubuntu 12.04.2 LTS (Precise Pangolin):
The Struts application is compiled with Java 7.
It turned out, someone uses "service tomcat [stop/start]" to restart Tomcat 7,
Which causes the "Unsupported major.minor version 51.0" error.
When we used "/etc/init.d/tomcat7 [stop/start]" to restart Tomcat 7, the problem was solved.
我解决了。我运行:
错误是误导性的,
不支持的Major.Minor版本51.0
。这给人的印象是不支持版本51(Java 7)。我们应该使用Java 6。错误应该是:
I solved it. I ran:
The error is misleading,
Unsupported major.minor version 51.0
. This gives the impression that version 51 (Java 7) is not supported. And we should use Java 6.The error should have been:
与您尝试使用的版本(较低的运行时版本)相比,您的Java文件具有不同的版本(更高的编译器版本)。
基本的理解是,以较低版本编译的类有望在更高的更高版本中运行。但是有时不可能相反(使用更高的编译器版本编译,并尝试使用较低的运行时版本运行它)。
因此,在尝试执行程序时,您会向您显示此错误。 不支持的专业。
(来源: oracle.com 。
Your Java file is compiled with a different version (higher compiler version) than the version (lower runtime version) you are trying to run it with.
It is basic understanding that classes compiled with lower versions are expected to run in the later higher versions. But the opposite (compiled with higher compiler version and trying to run it with lower runtime version) is quite not possible sometimes.
Hence you are shown this error, when trying to execute your program. Unsupported major.minor version x.x
(Source: oracle.com.)
OH Mac OS XI能够通过设置Java_home变量来解决此问题:
Oh Mac OS X I was able to solve this problem by setting the JAVA_HOME variable:
首先,让我们正确获取一些基础知识...
JRE是 eclipse /standalone,它将为您提供库,jvm,java plugins&amp; Java Web启动。请注意,它不提供合适的或辩论者。
JDK是JRE的超集团,以及合适的和辩论者。
因此,当您将默认库作为JRE而不是JDK时,您将有一个愉快的时间导入内容,但不会编译。
取而代之的是,将您的路径设置为JDK(我使用NetBeans,然后使用NetBeans/etc/NetBeans.conf中的NetBeans.Conf设置它们,然后更改路径)。
First let's get some basics right...
JRE is a component in NetBeans/Eclipse/standalone that is going to provide you with libraries, JVM, Java plugins & Java web start. Note that it does not provide compliers or debuggers.
JDK is the superset of JRE along with compliers and debuggers.
So when you have your default library as a JRE instead of JDK, you are going to have a nice time importing stuff, but it won't compile.
Instead, set your path to JDK (I use NetBeans, and I set them using netbeans.conf in netbeans/etc/netbeans.conf and change the path).