What version of javac built my jar?

前端 未结 20 915
孤街浪徒
孤街浪徒 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条回答
  •  Happy的楠姐
    2020-11-27 10:37

    The code posted by Owen can tell you the information mentioned by a number of the other answers here:

    public void simpleExample ()
    {
        FileInputStream fis = new FileInputStream ("mytest.class");
        parseJavaClassFile ( fis );
    }
    protected void parseJavaClassFile ( InputStream classByteStream ) throws Exception
    {
        DataInputStream dataInputStream = new DataInputStream ( classByteStream );
        magicNumber = dataInputStream.readInt();
        if ( magicNumber == 0xCAFEBABE )
        {
            int minorVer = dataInputStream.readUnsignedShort();
            int majorVer = dataInputStream.readUnsignedShort();
            // do something here with major & minor numbers
        }
    }
    

    See also this and this site. I ended up modifying the Mind Products code quickly to check what each of my dependencies was compiled for.

提交回复
热议问题