Monday, June 18, 2012

how to find byte code class file JDK compiled java version

you can find out the byte code class version.

On Linux, Mac OS X or Windows with Cygwin installed, the file(1) command knows the class version. Extract a class from a jar and use file to identify it:

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

A different class version, for example:

root@mypc:/merchant-sample/classes/com/paypal/core$ file SSLUtil.class 
SSLUtil.class: compiled Java class data, version 50.0 (Java 1.6)


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

46 = Java 1.2
47 = Java 1.3
48 = Java 1.4
49 = Java 5
50 = Java 6
51 = Java 7

Alternatively, you can make a simple test class that can identify class version.
import java.io.*;
 
public class Test {
    public static void main(String[] args) throws IOException {
        checkClassVersion("Class path you want to know");
    }                      
 
    private static void checkClassVersion(String filename)
        throws IOException
    {
        DataInputStream in = new DataInputStream
         (new FileInputStream(filename));
 
        int magic = in.readInt();
        if(magic != 0xcafebabe) {
          System.out.println(filename + " is not a valid class!");;
        }
        int minor = in.readUnsignedShort();
        int major = in.readUnsignedShort();
        System.out.println(filename + ": " + major + " . " + minor);
        in.close();
    }
}


No comments:

Post a Comment