简体   繁体   中英

How to tell the range of java versions a jar is compiled for

How to see which java versions a compiled jar file will work with?

Thanks

generally MANIFEST.MF file has this information as an attribute, if you don't find it, extract the jar and choose a class and do

$javap -verbose SomeClass.class  | grep 'major'
  major version: 50

and map the javac version from that major version

Here is the structure of a compiled java class file stated from this link :

http://en.wikipedia.org/wiki/Java_class_file

Sections[edit] There are 10 basic sections to the Java Class File structure:

  • Magic Number: 0xCAFEBABE
  • Version of Class File Format: the minor and major versions of the class file
  • Constant Pool: Pool of constants for the class
  • Access Flags: for example whether the class is abstract, static, etc.
  • This Class: The name of the current class
  • Super Class: The name of the super class
  • Interfaces: Any interfaces in the class
  • Fields: Any fields in the class
  • Methods: Any methods in the class
  • Attributes: Any attributes of the class (for example the name of the sourcefile, etc.)

As you can see, the second point is the version. Therefore, download an hex editor, open any .class file located in the jar and you will be able to read the version.

Edit : Altough I never verified, the byte offset for the version is suppose to be between 4 to 7, once again from the same link.

Edit 2 : If you prefer doing it with command, check this thread : how to check the jdk version used to compile a .class file

There is no such information in the Jar file, especially when considering that your term “java versions a compiled jar file will work with” heavily depends on how you define “will work”. An application could start, run for a second and then terminate with an exception. Does that already fulfill your definition of “does work”?

As said by others, there is a version number within the class files. You can find the information about how to map that version number to Java version here .

However, that version number only tells you the minimum JVM version that is needed to load that class file. It does not tell you which API it targets. It's perfectly legal to compile a class file compatible with a Java 1.1 JVM but using Java 8 APIs.

You could scan all class and member references of all class files within a Jar file and compare to the official API versions, however that only tells you which is effectively used, not what's intentionally targeted. Eg the application could still rely on certain bugs being fixed or missing functionality filled into already existing APIs. Eg whether an application relies on the requirement “the JRE's AWT can load PNG images with correct transparency support” can not be concluded by looking at the class file version number or at which API it refers to.

Specifying which Java version an application or library requires is beyond the scope of simple Jar files, eg you may have a look at OSGi or Java Webstart.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM