import java.io.FileInputStream;
public class ParseClassFile {
public static void main(String args[]) {
try {
// 讀取文件數(shù)據(jù),文件是當(dāng)前目錄下的First.class
FileInputStream fis = new FileInputStream("e:/logout_jsp.class");
int length = fis.available();
// 文件數(shù)據(jù)
byte[] data = new byte[length];
// 讀取文件到字節(jié)數(shù)組
fis.read(data);
// 關(guān)閉文件
fis.close();
// 解析文件數(shù)據(jù)
parseFile(data);
} catch (Exception e) {
System.out.println(e);
}
}
private static void parseFile(byte[] data) {
// 輸出魔數(shù)
System.out.print("魔數(shù)(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());
// 主版本號(hào)和次版本號(hào)碼
int minor_version = (((int) data[4]) << 8) + data[5];
int major_version = (((int) data[6]) << 8) + data[7];
System.out.println("版本號(hào)(version):" + major_version + "."
+ minor_version);
}
}
運(yùn)行:
E:\>javac ParseClassFile.java
E:\>java ParseClassFile
魔數(shù)(magic):0xCAFEBABE
版本號(hào)(version):48.0
48代表JDK1.5.0