参考下面两个文章对一个class文件进行解析:

http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.6

http://blog.163.com/hfut_quyouhu/blog/static/7847183520127214559314/

java的源代码如下:

 package test.java.abs;

 public class Abs {

     /**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub int a = -2;
System.out.println(abs(a));
} public static int abs(int i){
return ~i+1;
} }

编译之后对class 文件的分析结果如下:

CA FE BA BE ----> magic
00 00 -----> minor version
00 32 -----> major version
00 27 -----> constant pool count(39)

-------------------constant pool 1---------------
07 ----->CONSTANT_Class
00 02
-------------------constant pool 2---------------
01
00 11
74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62
73
test/java/abs/Abs
-------------------constant pool 3---------------
07
00 04
-------------------constant pool 4---------------
01
00 10
6A 61 76 61 2F 6C 61 6E 67 2F 4F 62 6A 65 63 74
java/lang/Object
-------------------constant pool 5---------------
01
00 06
3C 69 6E 69 74 3E
<init>
-------------------constant pool 6---------------
01
00 03
28 29 56
()V
-------------------constant pool 7---------------
01
00 04
43 6F 64 65
Code
-------------------constant pool 8---------------
0A
00 03
00 09
-------------------constant pool 9---------------
0C
00 05
00 06
-------------------constant pool 10---------------
01
00 0F
4C 69 6E 65 4E 75 6D 62 65 72 54 61 62 6C 65
LineNumberTable
-------------------constant pool 11---------------
01
00 12
4C 6F 63 61 6C 56 61 72 69 61 62 6C 65 54 61 62 6C 65
LocalVariableTable
-------------------constant pool 12---------------
01
00 04
74 68 69 73
this
-------------------constant pool 13---------------
01
00 13
4C 74 65 73 74 2F 6A 61 76 61 2F 61 62 73 2F 41 62 73 3B
Ltest/java/abs/Abs;
-------------------constant pool 14---------------
01
00 04
6D 61 69 6E
main
-------------------constant pool 15---------------
01
00 16
28 5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B 29 56
([Ljava/lang/String;)V
-------------------constant pool 16---------------
09
00 11
00 13
-------------------constant pool 17---------------
07
00 12
-------------------constant pool 18---------------
01
00 10
6A 61 76 61 2F 6C 61 6E 67 2F 53 79 73 74 65 6D
java/lang/System
-------------------constant pool 19---------------
0C
00 14
00 15
-------------------constant pool 20---------------
01
00 03
6F 75 74
out
-------------------constant pool 21---------------
01
00 15
4C 6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D 3B
Ljava/io/PrintStream;
-------------------constant pool 22---------------
0A
00 01
00 17
-------------------constant pool 23---------------
0C
00 18
00 19
-------------------constant pool 24---------------
01
00 03
61 62 73
abs
-------------------constant pool 25---------------
01
00 04
28 49 29 49
(I)I
-------------------constant pool 26---------------
0A
00 1B
00 1D
-------------------constant pool 27---------------
07
00 1C
-------------------constant pool 28---------------
01
00 13
6A 61 76 61 2F 69 6F 2F 50 72 69 6E 74 53 74 72 65 61 6D
java/io/PrintStream
-------------------constant pool 29---------------
0C
00 1E
00 1F
-------------------constant pool 30---------------
01
00 07
70 72 69 6E 74 6C 6E
println
-------------------constant pool 31---------------
01
00 04
28 49 29 56
(I)V
-------------------constant pool 32---------------
01
00 04
61 72 67 73
args
-------------------constant pool 33---------------
01
00 13
5B 4C 6A 61 76 61 2F 6C 61 6E 67 2F 53 74 72 69 6E 67 3B
[Ljava/lang/String;
-------------------constant pool 34---------------
01
00 01
61
a
-------------------constant pool 35---------------
01
00 01
49
I
-------------------constant pool 36---------------
01
00 01
69
i
-------------------constant pool 37---------------
01
00 0A
53 6F 75 72 63 65 46 69 6C 65
SourceFile
-------------------constant pool 38---------------
01
00 08
41 62 73 2E 6A 61 76 61
Abs.java

00 21 ------> ACC_PUBLIC,ACC_SUPER
00 01 ------> constant pool index 1
00 03 ------> super class
00 00 ------> interface count
00 00 ------> field count
00 03 ------> method count

-----------------method 1----------------------
00 01 ------> ACC_PUBLIC
00 05 ------> name_index <init>
00 06 ------> descriptor_index ()V
00 01 ------> attributes_count
        ----------------attribute 1 of method 1 --------------
        00 07 ------> attribute_name_index Code
        00 00 00 2F ------> attribute_length 47
        00 01 ------> max_stack
        00 01 ------> max_locals
        00 00 00 05 ------> code_length
        2A B7 00 08 B1
                -------------------------instructions----------------------
                2A aload_0 this
                B7 invokespecial 00 08 Object.init
                B1 return
        00 00 ------> exception_table_length
        00 02 ------> attributes_count
                ---------------------sub attribute 1-------------
                00 0A ------> attribute_name_index LineNumberTable
                00 00 00 06 ------> attribute_length
                00 01 ------> line_number_table_length
                00 00 ------> start_pc
                00 03 ------> line_number
                ---------------------sub attribute 2-------------
                00 0B ------> attribute_name_index LocalVariableTable
                00 00 00 0C ------> attribute_length
                00 01 ------> local_variable_table_length
                00 00 ------> start_pc
                00 05 ------> length
                00 0C ------> name_index this
                00 0D ------> descriptor_index Ltest/java/abs/Abs;
                00 00 ------> index

-----------------method 2----------------------
00 09 -------> ACC_PUBLIC,ACC_STATIC
00 0E -------> main
00 0F -------> ([Ljava/lang/String;)V
00 01 -------> attributes_count
        --------------------attribute 1 of method 2 -----------------------
        00 07 ------> attribute_name_index Code
        00 00 00 4A ------> attribute_length 74
        00 02 ------> max_stack
        00 02 ------> max_locals
        00 00 00 0E ------> code_length
        10 FE 3C B2 00 10 1B B8 00 16 B6 00 1A B1
                 -------------------------instructions----------------------
                10 bipush FE(-2)
                3C istore_1
                B2 getstatic 00 10 System.out
                1B iload_1
                B8 invokestatic 00 16 abs()
                B6 invokevirtual 00 1A println()
                B1 return
         00 00 ------> exception_table_length
         00 02 ------> attributes_count
                 ----------------------sub attribute 1 -----------------------
                 00 0A ----------> attribute_name_index LineNumberTable
                 00 00 00 0E ----------> attribute_length
                 00 03 ----------> line_number_table_length
                 00 00 ----------> start_pc
                 00 0B ----------> line_number
                 00 03 ----------> start_pc
                 00 0C ----------> line_number
                 00 0D ----------> start_pc
                 00 0D ----------> line_number
                 ----------------------sub attribute 2 -----------------------
                 00 0B ----------> attribute_name_index LocalVariableTable
                 00 00 00 16 ----------> attribute_length
                 00 02 ----------> local_variable_table_length
                 00 00 ----------> start_pc
                 00 0E ----------> length
                 00 20 ----------> name_index args
                 00 21 ----------> descriptor_index [Ljava/lang/String;
                 00 00 ----------> index 0
                 00 03 ----------> start_pc
                 00 0B ----------> length
                 00 22 ----------> name_index a
                 00 23 ----------> descriptor_index I
                 00 01 ----------> index 1

-----------------method 3----------------------
00 09 -------> ACC_PUBLIC,ACC_STATIC
00 18 -------> abs
00 19 -------> (I)I
00 01 -------> attribute_count

-------------------------attribute 1 of method 3 --------------------
00 07 ------> attribute_name_index Code
00 00 00 30 ------> attribute_lenght 48
00 02 ------> max_stack
00 01 ------> max_locals
00 00 00 06 ------> code_length
1A 02 82 04 60 AC

-------------------------instructions----------------------
1A iload_0
02 iconst_m1
82 ixor
04 iconst_1
60 iadd
AC ireturn

00 00 ------> exception_table_length

00 02 ------> attributes_count

------------------------sub attribute 1-----------------------
00 0A ----------> attribute_name_index LineNumberTable
00 00 00 06 ----------> attribute_length
00 01 ----------> line_number_table_length
00 00 ----------> start_pc
00 10 ----------> line_number
------------------------sub attribute 2-----------------------
00 0B ----------> attribute_name_index LocalVariableTable
00 00 00 0C ----------> attribute_length
00 01 ----------> local_varialbe_table_length
00 00 ----------> start_pc
00 06 ----------> length
00 24 ----------> name_index i
00 23 ----------> descriptor_index I
00 00 ----------> index

------------------------ class file attribute ----------------------------------
00 01 ---------> attribute count
00 25 ---------> attribute_name_index SourceFile
00 00 00 02 ---------> attribute_length
00 26 ---------> sourcefile_index Abs.java

java class 文件解析的更多相关文章

  1. javap -- Java 类文件解析器

    参考文档 http://blog.chinaunix.net/uid-692788-id-2681132.html http://docs.oracle.com/javase/7/docs/techn ...

  2. java 读文件 解析

    [Java]读取文件方法大全   1.按字节读取文件内容2.按字符读取文件内容3.按行读取文件内容 4.随机读取文件内容 public class ReadFromFile {     /**     ...

  3. Java XML文件解析

    四种生成和解析XML文档的方法详解(介绍+优缺点比较+示例) 蓝色字体内容由上一篇博文中补充进来的,写作风格比较好,大家有兴趣可以去查看原文 众所周知,现在解析XML的方法越来越多,但主流的方法也就四 ...

  4. java 写文件解析

    import java.io.File; import java.io.FileOutputStream; import java.io.*; public class FileTest { publ ...

  5. JAVA使用SAX解析XML文件

    在我的另一篇文章(http://www.cnblogs.com/anivia/p/5849712.html)中,通过一个例子介绍了使用DOM来解析XML文件,那么本篇文章通过相同的XML文件介绍如何使 ...

  6. java jar包解析:打包文件,引入文件

    java jar包解析:打包文件,引入文件 cmd下: jar命令:package包打包 javac命令:普通类文件打包 Hello.java: package org.lxh.demo; publi ...

  7. 《热血传奇2》wix、wil文件解析Java实现

    在百度上搜索java+wil只有iteye上一篇有丁点儿内容,不过他说的是错的!或者说是不完整的,我个人认为我对于热血传奇客户端解析还是有一定研究的,请移步: <JMir——Java版热血传奇2 ...

  8. java使用POI进行 Excel文件解析

    package com.timevale.esign.vip.util; import java.io.File; import java.io.FileInputStream; import jav ...

  9. Atitit。Tree文件解析器的原理流程与设计实现  java  c# php js

    Atitit.Tree文件解析器的原理流程与设计实现  java  c# php js 1. 解析原理与流程1 1.1. 判断目录  ,表示服  dirFlagChar = "└├─&quo ...

随机推荐

  1. netsh命令

    C:\Windows\System32>netsh interface ipv6 show address level=verbose 地址 ::1 参数-------------------- ...

  2. bzoj2506

    不知道该给这题贴什么标签了,但是这是一道好题1.注意这道题的询问是满足区间减法的,我们可以把一个询问拆成两个询问离线搞搞,这个思想在后面会见到2.分类的思想,这道题的求相同余数看似很烦,实际上我们发现 ...

  3. Linux Kernel ‘/net/socket.c’本地信息泄露漏洞

    漏洞名称: Linux Kernel ‘/net/socket.c’本地信息泄露漏洞 CNNVD编号: CNNVD-201312-037 发布时间: 2013-12-04 更新时间: 2013-12- ...

  4. POJ 3159 Candies 解题报告(差分约束 Dijkstra+优先队列 SPFA+栈)

    原题地址:http://poj.org/problem?id=3159 题意大概是班长发糖果,班里面有不良风气,A希望B的糖果不比自己多C个.班长要满足小朋友的需求,而且要让自己的糖果比snoopy的 ...

  5. WCF 双工模式

    WCF之消息模式分为:1.请求/答复模式2.单向模式3.双工模式 其中,请求/答复模式,在博文: WCF 入门教程一(动手新建第一个WCF程序并部署) WCF 入门教程二 中进行了详细介绍,此处将主要 ...

  6. JavaScript里的类和继承

    JavaScript与大部分客户端语言有几点明显的不同: JS是 动态解释性语言,没有编译过程,它在程序运行过程中被逐行解释执行JS是 弱类型语言,它的变量没有严格类型限制JS是面向对象语言,但 没有 ...

  7. iPhone丢失后通过iccid找回流程

    切记,丢失IPHONE第一时间要去补卡,免得小偷刷机用你的卡激活,这样你查到的号码只会是你自己的号码找iphone有2个方法:ICCID和维修换机.两者之间没任何直接联系,请不要混淆!下面是总体流程图 ...

  8. net 内存泄露和内存溢出

    一直以来都对内存泄露和内存溢出理解的不是很深刻.在网上看到了几篇文章,于是整理了一下自己对内存泄露和内存溢出的理解. 一.概念 内存溢出:指程序在运行的过程中,程序对内存的需求超过了超过了计算机分配给 ...

  9. Raspberry Pi使用USB摄像头远程监控

    用到了开源项目:MJPG-streamer 开源项目的下载地址: http://sourceforge.net/p/mjpg-streamer/code/HEAD/tree/mjpg-streamer ...

  10. scala-spark练手--dataframe数据可视化初稿

    成品:http://www.cnblogs.com/drawwindows/p/5640606.html 初稿: import org.apache.spark.sql.hive.HiveContex ...