how to check the jdk version used to compile a .class file [duplicate]
Asked Answered
S

5

336

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

Spinning answered 8/7, 2009 at 4:46 Comment(2)
See #698629Insessorial
Check out javadoc for more information on major and minor versions.Lalalalage
T
736

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
21 65
22 66
Taxicab answered 8/7, 2009 at 4:53 Comment(11)
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_layoutPlatonism
addendum: You can put -cp <jar> then the class so you can get it from and existing jarMckibben
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. #13357311Dorita
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.Roseanneroseate
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?Heavyladen
So, this major version is in sequence? so for next Java 12 coming march will be 56 ?Belen
The manifest Build-Jdk seems to be the JDK that made the jar file, which could be different from the JDK of the compiler.Wheatear
From inside your programSlack
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"Dominate
Could anybody add the major versions for Java >= 12?Smith
what about an aar?Cluny
M
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.

Masquerade answered 8/7, 2009 at 5:39 Comment(0)
S
9

Free JarCheck tool here

Suk answered 18/7, 2009 at 12:1 Comment(0)
I
6

You can try jclasslib:

https://github.com/ingokegel/jclasslib

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

Iseabal answered 2/8, 2011 at 7:37 Comment(0)
S
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?

Sadness answered 8/7, 2009 at 4:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.