321

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
  • 44,439
  • 22
  • 83
  • 99
  • See http://stackoverflow.com/questions/698129/how-can-i-find-the-target-java-version-for-a-compiled-class – Jon 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

706

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 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
phlogratos
  • 12,089
  • 1
  • 30
  • 37
John Calsbeek
  • 35,189
  • 7
  • 91
  • 100
  • 55
    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
  • You made my night. Thanks !!!! – fatherazrael May 19 '22 at 17:21
  • 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
  • 20,946
  • 3
  • 47
  • 62
9

Free JarCheck tool here

Gwyn Evans
  • 1,301
  • 7
  • 17
6

You can try jclasslib:

https://github.com/ingokegel/jclasslib

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

Vadzim
  • 23,055
  • 10
  • 129
  • 146
marekdef
  • 471
  • 4
  • 9
2

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
  • 811,175
  • 162
  • 1,198
  • 1,373