333

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

I'm trying to debug a

"Bad version number in .class file'

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

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

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

2

5 Answers 5

727

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

On Unix/Linux:

javap -verbose MyClass | grep "major"

On Windows:

javap -verbose MyClass | findstr "major"

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

Java Version Major Version
1.2 46
1.3 47
1.4 48
5 49
6 50
7 51
8 52
9 53
10 54
11 55
12 56
13 57
14 58
15 59
16 60
17 61
18 62
19 63
20 64
11
  • 57
    Alternatively, if you open the class file in a hex editor you can look at bytes 7 and 8. The number will map to the numbers given in the above answer. E.g. 00 2E -> JDK 1.2 = 46 (0x2E hex). Useful if you don't have access to javap. ref: en.wikipedia.org/wiki/Java_class_file#General_layout
    – Jim
    Apr 24, 2014 at 11:19
  • 23
    addendum: You can put -cp <jar> then the class so you can get it from and existing jar Dec 4, 2014 at 17:32
  • 3
    I had to remove the ".class" in the command otherwise I got a message, "Error: Cannot find foo.class". So doing, "javap -verbose foo | grep "major"" worked. Just a heads up. stackoverflow.com/questions/13356811/… Dec 1, 2015 at 19:21
  • 2
    As Jim advises, for vi addicts: "vi MyClass" then "<Esc> :!xxd" then peep at byte 8; 0x31, 0x32, 0x33, 0x34, 0x35 stands for java 5, 6, 7, 8, 9 respectively. "<Esc> :q!" to exit vi. Sep 9, 2017 at 14:28
  • 1
    So I was working on a project that uses amazon sdk. The project uses Java 1.7. If I check inside of the MANIFEST.MF of amazon sdk it says Build-Jdk: 1.8.0_111 , but if I use javap -verbose MyClass | grep "major" it says major version: 50 which means Java Version 6. This is confusing. Does that mean - even though it was compiled with 8 but it can run on 6? Nov 20, 2018 at 13:32
17

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.

0
9

Free JarCheck tool here

0
6

You can try jclasslib:

https://github.com/ingokegel/jclasslib

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

3

Does the -verbose flag to your java command yield any useful info? If not, maybe java -X reveals something specific to your version that might help?

Not the answer you're looking for? Browse other questions tagged or ask your own question.