Java 如何检查用于编译 .class 文件的 jdk 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1096148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:39:35  来源:igfitidea点击:

how to check the jdk version used to compile a .class file

java

提问by Charles Ma

Possible Duplicate:
Tool to read and display Java .class versions

可能的重复:
读取和显示 Java .class 版本的工具

I'm trying to debug a

我正在尝试调试一个

"Bad version number in .class file'

“.class 文件中的错误版本号”

error in java, is there a way for me to check which version the .classfiles are?

java中的错误,有没有办法让我检查.class文件是哪个版本?

I'm using JRE1.5.0_6, but my JDKis version 1.6.0_13.

我正在使用JRE1.5.0_6,但我JDK的版本是 1.6.0_13。

I'm compiling with compatibility mode set to 1.5 in eclipse which I thought would work...

我在 eclipse 中将兼容模式设置为 1.5 进行编译,我认为这会起作用...

采纳答案by John Calsbeek

You're looking for this on the command line (for a class called MyClass):

您正在命令行上查找此内容(对于名为 MyClass 的类):

On Unix/Linux:

在 Unix/Linux 上:

javap -verbose MyClass | grep "major"

On Windows:

在 Windows 上:

javap -verbose MyClass | findstr "major"

You want the majorversion from the results. Here are some example values:

您需要结果中的主要版本。以下是一些示例值:

  • Java 1.2 uses major version 46
  • Java 1.3 uses major version 47
  • Java 1.4 uses major version 48
  • Java 5 uses major version 49
  • Java 6 uses major version 50
  • Java 7 uses major version 51
  • Java 8 uses major version 52
  • Java 9 uses major version 53
  • Java 10 uses major version 54
  • Java 11 uses major version 55
  • Java 1.2 使用主要版本 46
  • Java 1.3 使用主要版本 47
  • Java 1.4 使用主要版本 48
  • Java 5 使用主要版本 49
  • Java 6 使用主要版本 50
  • Java 7 使用主要版本 51
  • Java 8 使用主要版本 52
  • Java 9 使用主要版本 53
  • Java 10 使用主要版本 54
  • Java 11 使用主要版本 55

回答by Alex Martelli

Does the -verboseflag to your javacommand yield any useful info? If not, maybe java -Xreveals something specific to your version that might help?

-verbose您的java命令的标志是否会产生任何有用的信息?如果没有,也许会java -X揭示一些特定于您的版本可能会有所帮助的内容?

回答by Sean Reilly

Btw, the reason that you're having trouble is that the java compiler recognizes two version flags. There is -source 1.5, which assumes java 1.5 level source code, and -target 1.5, which will emit java 1.5 compatible class files. You'll probably want to use both of these switches, but you definitely need -target 1.5; try double checking that eclipse is doing the right thing.

顺便说一句,您遇到问题的原因是 java 编译器识别两个版本标志。有 -source 1.5,它假定 java 1.5 级别的源代码,和 -target 1.5,它将发出 java 1.5 兼容的类文件。您可能想要同时使用这两个开关,但您绝对需要 -target 1.5;尝试仔细检查 eclipse 是否在做正确的事情。

回答by Gwyn Evans

Free JarCheck tool here

免费 JarCheck 工具在这里

回答by marekdef

You can try jclasslib:

你可以试试jclasslib:

https://github.com/ingokegel/jclasslib

https://github.com/ingokegel/jclasslib

It's nice that it can associate itself with *.class extension.

很高兴它可以将自己与 *.class 扩展名关联。