Java API to find out the JDK version a class file

2019-01-07 09:21发布

Are there any Java APIs to find out the JDK version a class file is compiled for? Of course there is the javap tool to find out the major version as mentioned in here. However I want to do it programmatically so that that I could warn the user to compile it for the appropriate JDK

标签: version java
8条回答
Explosion°爆炸
2楼-- · 2019-01-07 09:52

basszero's approach can be done via the UNIX command line, and the "od(1)" command:

% od -x HelloWorldJava.class |head -2
0000000 feca beba 0000 3100 dc00 0007 0102 2a00
0000020 6f63 2f6d 6e65 6564 6163 642f 6d65 2f6f

"feca beba" is the magic number. The "0000 3100" is 0x31, which represents J2SE 5.0.

查看更多
戒情不戒烟
3楼-- · 2019-01-07 09:54

Just read the class file directly. It's VERY easy to figure out the version. Check out the the spec and wiki and then try the code. Wrapping this code and making is more useful/pretty is left as an exercise. Alternatively, you could use a library like BCEL, ASM, or JavaAssist

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

public class ClassVersionTest
{
    private static final int JAVA_CLASS_MAGIC = 0xCAFEBABE;

    public static void main(String[] args)
    {
        try
        {
            DataInputStream dis = new DataInputStream(new FileInputStream("Test.class"));
            int magic = dis.readInt();
            if(magic == JAVA_CLASS_MAGIC)
            {
                int minorVersion = dis.readUnsignedShort();
                int majorVersion = dis.readUnsignedShort();

                /**
                 * majorVersion is ...
                 * 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).
                 */

                System.out.println("ClassVersionTest.main() " + majorVersion + "." + minorVersion);
            }
            else
            {
                // not a class file
            }
        }
        catch (FileNotFoundException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}
查看更多
看我几分像从前
4楼-- · 2019-01-07 09:57

JavaP does have an API, but it's specific to the Sun JDK.

It's found in tools.jar, under sun/tools/javap/Main.class.

查看更多
干净又极端
5楼-- · 2019-01-07 09:59

Apache BCEL provides this API:

JavaClass c = Repository.lookupClass("com.x.MyClass")
c.getMinor();
c.getMajor();
查看更多
疯言疯语
6楼-- · 2019-01-07 10:01

On Linux, you can use the file command on a class file, which I found easiest for my use case. For example:

$ file Foo.class
Foo.class: compiled Java class data, version 50.0 (Java 1.6)
查看更多
混吃等死
7楼-- · 2019-01-07 10:03

As others have shown, it is easy enough to do by reading the first eight bytes of a class file. If you want a pre-built binary library, you can download one here.

查看更多
登录 后发表回答