@yudesong
2018-02-12T03:06:10.000000Z
字数 3015
阅读 719
字节码
首先,来编写一个简单的HelloWorld.java程序
public class HelloWorld {public static void main(String[] args) {System.out.println("Hello World");}}
看看.class文件的内容吧
cafe babe 0000 0034 001d 0a00 0600 0f090010 0011 0800 120a 0013 0014 0700 15070016 0100 063c 696e 6974 3e01 0003 28295601 0004 436f 6465 0100 0f4c 696e 654e756d 6265 7254 6162 6c65 0100 046d 61696e01 0016 285b 4c6a 6176 612f 6c61 6e672f53 7472 696e 673b 2956 0100 0a53 6f757263 6546 696c 6501 000f 4865 6c6c 6f576f72 6c64 2e6a 6176 610c 0007 0008 0700170c 0018 0019 0100 0b48 656c 6c6f 20576f72 6c64 0700 1a0c 001b 001c 0100 0a48656c 6c6f 576f 726c 6401 0010 6a61 76612f6c 616e 672f 4f62 6a65 6374 0100 106a6176 612f 6c61 6e67 2f53 7973 7465 6d010003 6f75 7401 0015 4c6a 6176 612f 696f2f50 7269 6e74 5374 7265 616d 3b01 00136a61 7661 2f69 6f2f 5072 696e 7453 74726561 6d01 0007 7072 696e 746c 6e01 0015284c 6a61 7661 2f6c 616e 672f 5374 72696e67 3b29 5600 2100 0500 0600 0000 00000200 0100 0700 0800 0100 0900 0000 1d000100 0100 0000 052a b700 01b1 0000 0001000a 0000 0006 0001 0000 0001 0009 000b000c 0001 0009 0000 0025 0002 0001 00000009 b200 0212 03b6 0004 b100 0000 01000a00 0000 0a00 0200 0000 0300 0800 04000100 0d00 0000 0200 0e
ClassFile结构
| 类型 | 名称 | 数量 |
|---|---|---|
| u4 | magic | 1 |
| u2 | minor_version | 1 |
| u2 | major_version | 1 |
| u2 | constant_pool_count | 1 |
| cp_info | constant_pool | constant_pool_count-1 |
| u2 | access_flags | 1 |
| u2 | this_class | 1 |
| u2 | super_class | 1 |
| u2 | interfaces_count | 1 |
| u2 | interfaces | interfaces_count |
| u2 | fields_count | 1 |
| field_info | fields | fields_count |
| u2 | methods_count | 1 |
| method_info | methods | methods_count |
| u2 attributes_count | 1 | |
| attribute_info | attributes | attributes_count |
javap -verbose HelloWorld.class
Classfile /Users/yudesong/Project/java/HelloWorld.classLast modified 2018-1-23; size 425 bytesMD5 checksum 63e47f1d243e0eb6bc952df3f6ac0d5aCompiled from "HelloWorld.java"public class HelloWorldminor version: 0major version: 52flags: ACC_PUBLIC, ACC_SUPERConstant pool:#1 = Methodref #6.#15 // java/lang/Object."<init>":()V#2 = Fieldref #16.#17 // java/lang/System.out:Ljava/io/PrintStream;#3 = String #18 // Hello World#4 = Methodref #19.#20 // java/io/PrintStream.println:(Ljava/lang/String;)V#5 = Class #21 // HelloWorld#6 = Class #22 // java/lang/Object#7 = Utf8 <init>#8 = Utf8 ()V#9 = Utf8 Code#10 = Utf8 LineNumberTable#11 = Utf8 main#12 = Utf8 ([Ljava/lang/String;)V#13 = Utf8 SourceFile#14 = Utf8 HelloWorld.java#15 = NameAndType #7:#8 // "<init>":()V#16 = Class #23 // java/lang/System#17 = NameAndType #24:#25 // out:Ljava/io/PrintStream;#18 = Utf8 Hello World#19 = Class #26 // java/io/PrintStream#20 = NameAndType #27:#28 // println:(Ljava/lang/String;)V#21 = Utf8 HelloWorld#22 = Utf8 java/lang/Object#23 = Utf8 java/lang/System#24 = Utf8 out#25 = Utf8 Ljava/io/PrintStream;#26 = Utf8 java/io/PrintStream#27 = Utf8 println#28 = Utf8 (Ljava/lang/String;)V{public HelloWorld();descriptor: ()Vflags: ACC_PUBLICCode:stack=1, locals=1, args_size=10: aload_01: invokespecial #1 // Method java/lang/Object."<init>":()V4: returnLineNumberTable:line 1: 0public static void main(java.lang.String[]);descriptor: ([Ljava/lang/String;)Vflags: ACC_PUBLIC, ACC_STATICCode:stack=2, locals=1, args_size=10: getstatic #2 // Field java/lang/System.out:Ljava/io/PrintStream;3: ldc #3 // String Hello World5: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/String;)V8: returnLineNumberTable:line 3: 0line 4: 8}SourceFile: "HelloWorld.java"
建议直接安装IDEA 插件jclasslib