Java - Which version was a .jar file built too? [duplicate]
Is there a way to find out the JDK version used to build开发者_JAVA百科 a .jar file?
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.
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.
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.
In the Jars MANIFEST.MF there may be a Build-Jdk property which should be what you're looking for.
精彩评论