332

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...

Community
  • 1
  • 1
Charles Ma
  • 47,141
  • 22
  • 87
  • 101
  • See http://stackoverflow.com/questions/698129/how-can-i-find-the-target-java-version-for-a-compiled-class – Jonathan Holloway Jul 08 '09 at 04:58
  • Check out [javadoc](http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html) for more information on major and minor versions. – WiseGeek Jul 08 '09 at 05:33

5 Answers5

726

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
Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
John Calsbeek
  • 35,947
  • 7
  • 94
  • 101
  • 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: http://en.wikipedia.org/wiki/Java_class_file#General_layout – Jim Apr 24 '14 at 11:19
  • 23
    addendum: You can put -cp then the class so you can get it from and existing jar – Christian Bongiorno Dec 04 '14 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. http://stackoverflow.com/questions/13356811/java-javap-errorcould-not-find-class – Kyle Bridenstine Dec 01 '15 at 19:21
  • 2
    As Jim advises, for vi addicts: "vi MyClass" then " :!xxd" then peep at byte 8; 0x31, 0x32, 0x33, 0x34, 0x35 stands for java 5, 6, 7, 8, 9 respectively. " :q!" to exit vi. – user3526918 Sep 09 '17 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? – Paramvir Singh Karwal Nov 20 '18 at 13:32
  • So, this major version is in sequence? so for next Java 12 coming march will be 56 ? – DAIRAV Nov 27 '18 at 07:29
  • 1
    The manifest Build-Jdk seems to be the JDK that made the jar file, which could be different from the JDK of the compiler. – Bruce Sep 22 '19 at 09:09
  • [From inside your program](https://stackoverflow.com/questions/27065/tool-to-read-and-display-java-class-versions/58642950#58642950) – Marinos An Oct 31 '19 at 12:13
  • Just for information, On Windows we can use following command to find major version of jdk with which the class was compiled: javap -verbose Xyz.class | findstr "major" – Fahim Aslam Bhatti Nov 07 '19 at 13:12
  • Could anybody add the major versions for Java >= 12? – Matthias Boehm Feb 07 '22 at 15:17
  • what about an aar? – Fran Marzoa May 30 '22 at 14:20
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.

Sean Reilly
  • 21,526
  • 4
  • 48
  • 62
9

Free JarCheck tool here

Gwyn Evans
  • 1,361
  • 7
  • 17
6

You can try jclasslib:

https://github.com/ingokegel/jclasslib

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

Vadzim
  • 24,954
  • 11
  • 143
  • 151
marekdef
  • 481
  • 4
  • 10
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?

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395