如何修复java.lang.UnsupportedClassVersionError:不支持的major.minor版本

本文翻译自:How to fix java.lang.UnsupportedClassVersionError: Unsupported major.minor version

I am trying to use Notepad++ as my all-in-one tool edit, run, compile, etc. 我正在尝试使用Notepad ++作为我的多合一工具进行编辑,运行,编译等。

I have JRE installed, and I have setup my path variable to the .../bin directory. 我已经安装了JRE ,并且已将路径变量设置到.../bin目录。

When I run my "Hello world" in Notepad++, I get this message: 当我在Notepad ++中运行“ 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)
       .........................................

I think the problem here is about versions; 我认为这里的问题是关于版本的。 some versions of Java may be old or too new. Java的某些版本可能太旧或太新。

  1. How do I fix it? 我如何解决它?
  2. Should I install the JDK, and setup my path variable to the JDK instead of JRE? 我应该安装JDK,并将路径变量设置为JDK而不是JRE吗?
  3. What is the difference between the PATH variable in JRE or JDK? JRE或JDK中的PATH变量之间有什么区别?

#1楼

参考:https://stackoom.com/question/hZ4b/如何修复java-lang-UnsupportedClassVersionError-不支持的major-minor版本


#2楼

Don't worry, I got it solved. 不用担心,我已经解决了。

It is actually simple - you need to install BOTH JRE / JDK with the same version. 这实际上很简单-您需要同时安装相同版本的JRE / JDK。

JRE 6 -> JDK 6 JRE 6-> JDK 6

JRE 7 -> JDK 7 JRE 7-> JDK 7

And so on. 等等。


#3楼

发生java.lang.UnsupportedClassVersionError原因是,编译时JDK较高,而运行时JDK较低。


#4楼

The version number shown describes the version of the JRE the class file is compatible with. 显示的版本号描述了与类文件兼容的JRE的版本。

The reported major numbers are: 报告的主要数字是:

Java SE 13 = 57,
Java SE 12 = 56,
Java SE 11 = 55,
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

(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. 要解决实际问题,您应该尝试使用Java JRE的较新版本运行Java代码,或为Java编译器指定目标参数,以指示编译器创建与Java早期版本兼容的代码。

For example, in order to generate class files compatible with Java 1.4, use the following command line: 例如,为了生成与Java 1.4兼容的类文件,请使用以下命令行:

javac -target 1.4 HelloWorld.java

With newer versions of the Java compiler you are likely to get a warning about the bootstrap class path not being set. 使用较新版本的Java编译器,您可能会收到有关未设置引导程序类路径的警告。 More information about this error is available in a blog post New javac warning for setting an older source without bootclasspath . 有关此错误的更多信息,请参见博客文章New javac warning有关设置没有bootclasspath的较旧源的警告


#5楼

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. 此错误表示您正在尝试加载使用比您所安装的Java更高版本编译的Java“类”文件。

For example, your .class file could have been compiled for JDK 7, and you're trying to run it with JDK 6. 例如,您的.class文件可能已经针对JDK 7进行了编译,而您正在尝试使用JDK 6运行它。

So the solution is to either: 因此,解决方案是:

  • Upgrade your Java runtime or 升级Java运行时或
  • Recompile the class if you have the source, using your local Java compiler (if you have one). 如果有源,请使用本地Java编译器(如果有)重新编译该类。

    javac FileName.java 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! 对于开发人员,如果其他开发人员签入.class文件,并且他们拥有比您更高的Java版本,则可能会发生这种情况!


#6楼

在Eclipse中,我只是转到菜单命令窗口 -> 首选项 -> Java- > 编译器 ,然后将“编译器遵从级别”设置为1.6。

发布了0 篇原创文章 · 获赞 7 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/asdfgh0077/article/details/105215599
今日推荐