What version of javac built my jar?

前端 未结 20 889
孤街浪徒
孤街浪徒 2020-11-27 09:48

How can I tell what version of the Java compiler was used to build a jar? I have a jar file, and it could have been built in any one of three JDKs. We need to know exactly

20条回答
  •  [愿得一人]
    2020-11-27 10:43

    A jar is merely a container. It is a file archive ā la tar. While a jar may have interesting information contained within it's META-INF hierarchy, it has no obligation to specify the vintage of the classes within it's contents. For that, one must examine the class files therein.

    As as Peter Lawrey mentioned in comment to the original question, you can't necessarily know which JDK release built a given class file, but you can find out the byte code class version of the class file contained in a jar.

    Yes, this kinda sucks, but the first step is to extract one or more classes from the jar. For example:

    $ jar xf log4j-1.2.15.jar
    

    On Linux, Mac OS X or Windows with Cygwin installed, the file(1) command knows the class version.

    $ file ./org/apache/log4j/Appender.class
    ./org/apache/log4j/Appender.class: compiled Java class data, version 45.3
    

    Or alternatively, using javap from the JDK as @jikes.thunderbolt aptly points out:

    $ javap -v ./org/apache/log4j/Appender.class | grep major
     major version: 45
    

    For Windows environments without either file or grep

    > javap -v ./org/apache/log4j/Appender.class | findstr major
     major version: 45
    

    FWIW, I will concur that javap will tell a whole lot more about a given class file than the original question asked.

    Anyway, a different class version, for example:

    $ file ~/bin/classes/P.class
    /home/dave/bin/classes/P.class: compiled Java class data, version 50.0
    

    The class version major number corresponds to the following Java JDK versions:

    • 45.3 = Java 1.1
    • 46 = Java 1.2
    • 47 = Java 1.3
    • 48 = Java 1.4
    • 49 = Java 5
    • 50 = Java 6
    • 51 = Java 7
    • 52 = Java 8
    • 53 = Java 9
    • 54 = Java 10
    • 55 = Java 11
    • 56 = Java 12
    • 57 = Java 13
    • 58 = Java 14

提交回复
热议问题