JBOSS 點滴

          豐豐的博客

          通過class文件,看版本

          import java.io.FileInputStream;

          public class ParseClassFile {

              public static void main(String args[]) {

                  try {

                      // 讀取文件數據,文件是當前目錄下的First.class

                      FileInputStream fis = new FileInputStream("e:/logout_jsp.class");

                      int length = fis.available();

                      // 文件數據

                      byte[] data = new byte[length];

                      // 讀取文件到字節數組

                      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);

              }

          }

           

          運行:

          E:\>javac ParseClassFile.java

          E:\>java  ParseClassFile
          魔數(magic):0xCAFEBABE
          版本號(version):48.0
          48代表JDK1.5.0

          posted on 2009-07-16 11:25 半導體 閱讀(384) 評論(0)  編輯  收藏 所屬分類: Eclipse

          主站蜘蛛池模板: 抚顺市| 凤庆县| 芒康县| 西畴县| 长沙市| 大竹县| 佛学| 牙克石市| 噶尔县| 盈江县| 荆门市| 荔波县| 晴隆县| 邵阳县| 防城港市| 琼结县| 平南县| 五莲县| 义马市| 鄂尔多斯市| 上饶市| 邢台市| 凤山县| 阳泉市| 高雄市| 吉安市| 吐鲁番市| 垦利县| 涞水县| 晋江市| 大姚县| 买车| 河曲县| 安吉县| 长沙县| 平南县| 万宁市| 青川县| 会理县| 垫江县| 鹤峰县|