jvm - class文件?

方案一:
class文件都是二进制的。
首先用vi 打开
然后输入%!xxd
显示其16进制格式

方案二:
javap -verbose xxx.class

例子

原始代码

public class SymjaException extends CalculatorException {

    public SymjaException(String message, IExceptionType type) {
        super(message, type);
    }
}

字节码(16进制)

  1 0000000: cafe babe 0000 0034 0014 0a00 0300 1107  .......4........
  2 0000010: 0012 0700 1301 0006 3c69 6e69 743e 0100  ..........
  3 0000020: 4128 4c6a 6176 612f 6c61 6e67 2f53 7472  A(Ljava/lang/Str
  4 0000030: 696e 673b 4c6f 7574 666f 782f 6361 6c63  ing;Loutfox/calc
  5 0000040: 756c 6174 6f72 2f65 7863 6570 7469 6f6e  ulator/exception
  6 0000050: 2f49 4578 6365 7074 696f 6e54 7970 653b  /IExceptionType;
  7 0000060: 2956 0100 0443 6f64 6501 000f 4c69 6e65  )V...Code...Line
  8 0000070: 4e75 6d62 6572 5461 626c 6501 0012 4c6f  NumberTable...Lo
  9 0000080: 6361 6c56 6172 6961 626c 6554 6162 6c65  calVariableTable
 10 0000090: 0100 0474 6869 7301 002c 4c6f 7574 666f  ...this..,Loutfo
 11 00000a0: 782f 6361 6c63 756c 6174 6f72 2f65 7863  x/calculator/exc
 12 00000b0: 6570 7469 6f6e 2f53 796d 6a61 4578 6365  eption/SymjaExce
 13 00000c0: 7074 696f 6e3b 0100 076d 6573 7361 6765  ption;...message
 14 00000d0: 0100 124c 6a61 7661 2f6c 616e 672f 5374  ...Ljava/lang/St
 15 00000e0: 7269 6e67 3b01 0004 7479 7065 0100 2c4c  ring;...type..,L
 16 00000f0: 6f75 7466 6f78 2f63 616c 6375 6c61 746f  outfox/calculato
 17 0000100: 722f 6578 6365 7074 696f 6e2f 4945 7863  r/exception/IExc
 18 0000110: 6570 7469 6f6e 5479 7065 3b01 000a 536f  eptionType;...So
 19 0000120: 7572 6365 4669 6c65 0100 1353 796d 6a61  urceFile...Symja
 20 0000130: 4578 6365 7074 696f 6e2e 6a61 7661 0c00  Exception.java..
 21 0000140: 0400 0501 002a 6f75 7466 6f78 2f63 616c  .....*outfox/cal
 22 0000150: 6375 6c61 746f 722f 6578 6365 7074 696f  culator/exceptio
 23 0000160: 6e2f 5379 6d6a 6145 7863 6570 7469 6f6e  n/SymjaException
 24 0000170: 0100 2f6f 7574 666f 782f 6361 6c63 756c  ../outfox/calcul
 25 0000180: 6174 6f72 2f65 7863 6570 7469 6f6e 2f43  ator/exception/C
 26 0000190: 616c 6375 6c61 746f 7245 7863 6570 7469  alculatorExcepti
 27 00001a0: 6f6e 0021 0002 0003 0000 0000 0001 0001  on.!............
 28 00001b0: 0004 0005 0001 0006 0000 0049 0003 0003  ...........I....
 29 00001c0: 0000 0007 2a2b 2cb7 0001 b100 0000 0200  ....*+,.........
 30 00001d0: 0700 0000 0a00 0200 0000 1100 0600 1200  ................
 31 00001e0: 0800 0000 2000 0300 0000 0700 0900 0a00  .... ...........
 32 00001f0: 0000 0000 0700 0b00 0c00 0100 0000 0700  ................
 33 0000200: 0d00 0e00 0200 0100 0f00 0000 0200 100a  ................

javap

  MD5 checksum 0ae899847b450473f19c09f6e38899e2
  Compiled from "SymjaException.java"
public class outfox.calculator.exception.SymjaException extends outfox.calculator.exception.CalculatorException
  minor version: 0
  major version: 52
  flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
   #1 = Methodref          #3.#17         // outfox/calculator/exception/CalculatorException."":(Ljava/lang/String;Loutfox/calculator/exception/IExceptionType;)V
   #2 = Class              #18            // outfox/calculator/exception/SymjaException
   #3 = Class              #19            // outfox/calculator/exception/CalculatorException
   #4 = Utf8               







   #5 = Utf8               (Ljava/lang/String;Loutfox/calculator/exception/IExceptionType;)V
   #6 = Utf8               Code
   #7 = Utf8               LineNumberTable
   #8 = Utf8               LocalVariableTable
   #9 = Utf8               this
  #10 = Utf8               Loutfox/calculator/exception/SymjaException;
  #11 = Utf8               message
  #12 = Utf8               Ljava/lang/String;
  #13 = Utf8               type
  #14 = Utf8               Loutfox/calculator/exception/IExceptionType;
  #15 = Utf8               SourceFile
  #16 = Utf8               SymjaException.java
  #17 = NameAndType        #4:#5          // "":(Ljava/lang/String;Loutfox/calculator/exception/IExceptionType;)V
  #18 = Utf8               outfox/calculator/exception/SymjaException
  #19 = Utf8               outfox/calculator/exception/CalculatorException
{
  public outfox.calculator.exception.SymjaException(java.lang.String, outfox.calculator.exception.IExceptionType);
    descriptor: (Ljava/lang/String;Loutfox/calculator/exception/IExceptionType;)V
    flags: ACC_PUBLIC
    Code:
      stack=3, locals=3, args_size=3
         0: aload_0
         1: aload_1
         2: aload_2
         3: invokespecial #1                  // Method outfox/calculator/exception/CalculatorException."":(Ljava/lang/String;Loutfox/calculator/exception/IExceptionType;)V
         6: return
      LineNumberTable:
        line 17: 0
        line 18: 6
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0       7     0  this   Loutfox/calculator/exception/SymjaException;
            0       7     1 message   Ljava/lang/String;
            0       7     2  type   Loutfox/calculator/exception/IExceptionType;
}
SourceFile: "SymjaException.java"

可以根据字节码(16进制),一步步的读取一下数据。就能了解了。

简要分区

1、魔数,版本号
2、常量池
3、类索引,父类索引以及接口索引(3个0x0000这种格式表示)
4、字段(没有的话就是0x0000)比如上面的例子
5、方法(没有的话就是0x0000)
6、属性(会引用常量池的索引)

你可能感兴趣的:(jvm - class文件?)