读取CLASS文件JDK版本
package com.tool.classversion;
import java.io.FileInputStream;
/*
* (magic):0xCAFEBABE
// 5.0
// (version):49.0
// 6.0
// (version):50.0
// 1.4
// (version):46.0
// 1.3
// (version):45.3
* */
public class JavaVersionUtil {
public static void main(String args[]) {
String fileName = "PolicyInterfaceReqModel.class";
printVersion(fileName);
}
public static void printVersion(String fileName) {
try {
FileInputStream fis = new FileInputStream(fileName);
byte[] data = new byte[8];
fis.read(data);
fis.close();
parseFile(data);
} catch (Exception e) {
System.out.println(e);
}
}
private static void parseFile(byte[] data) {
System.out.print("(magic):0x");
System.out.print(Integer.toHexString(data[0]).substring(6).toUpperCase());
System.out.print(Integer.toHexString(data[1]).substring(6).toUpperCase());
System.out.print(Integer.toHexString(data[2]).substring(6).toUpperCase());
System.out.println(Integer.toHexString(data[3]).substring(6).toUpperCase());
int minor_version = (((int) data[4]) << 8) + data[5];
int major_version = (((int) data[6]) << 8) + data[7];
System.out.println("(version):" + major_version + "."+ minor_version);
}
}