12

Is there a way to find out the JDK version used to build a .jar file?

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Ash Burlaczenko
  • 24,778
  • 15
  • 68
  • 99

4 Answers4

12

That's not possible reliably, since you don't even need a JDK to build a JAR file - it's just a ZIP file with the classes and a manifest file inside.

What you can find out is what version of the class file format is used. The major version number maps to majorJDK releases:

J2SE 6.0 = 50 (0x32 hex)
J2SE 5.0 = 49 (0x31 hex)
JDK 1.4 = 48 (0x30 hex)
JDK 1.3 = 47 (0x2F hex)
JDK 1.2 = 46 (0x2E hex)
JDK 1.1 = 45 (0x2D hex)

However, the java compiler has an option to use the class file format of a previous version, which is frequently used to achieve downwards compatibility. But if you find a class file version major number of 50, you know that the classes were definitely not compiled with a Java 5 or earlier JDK.

Michael Borgwardt
  • 342,105
  • 78
  • 482
  • 720
  • 1
    While everything you say is correct, this really tells you what compiler compiled your classes, not what jar tool jar-ed your classes. Most of the time one would assume that they come from the same toolkit. – Edwin Buck Jan 12 '11 at 19:42
  • The answer http://stackoverflow.com/a/3313839/2776843 is much better. Check that too. – 99problems Nov 03 '16 at 08:50
10

Most jars are built using the provided "jar" tool packaged with the jdk.

In SUN's JDK the provided "jar" tool will add a "Created-By" line in the embedded META-INF/MANIFEST.MF file.

That line will specify the version of the JDK that created the JAR (in my case "Created-By: 1.6.0_17 (Sun Microsystems Inc.)")

Other Java vendors tend to do the same.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • Does the Ant jar task use the jar tool internally? Because I think rather more JAR files are built with Ant than via the command line tool. – Michael Borgwardt Dec 14 '10 at 15:54
9

You can extract the class files from the jar file and use the following command:

javap -verbose SomeClass | grep major

Should give you the version number of the class files.

Nayeem Zen
  • 2,569
  • 1
  • 17
  • 16
7

In the Jars MANIFEST.MF there may be a Build-Jdk property which should be what you're looking for.

Jim
  • 22,354
  • 6
  • 52
  • 80
  • I just jar'd up a few class files and Build-Jdk is not in the MANIFEST.MF file at all. – Edwin Buck Dec 14 '10 at 15:47
  • 1
    Mmm, you may be right. I'm looking at a jar with Created-By: Apache Maven. The docs seems to agree with you however. – Jim Dec 14 '10 at 15:50