参考下面两个文章对一个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. vc:如何从Internet上有效而稳定地下载文件

    http://www.vckbase.com/index.php/wv/172 如何从Internet上有效而稳定地下载文件 ,这是很多网络应用程序要考虑的重要问题,本文提供的代码段针对这个问题进行了 ...

  2. hadoop2.2编程: SequenceFileWritDemo

    import java.io.IOException; import java.net.URI; import org.apache.hadoop.fs.FileSystem; import org. ...

  3. js中字符串转换为日期和比较大小

    本文转载于:http://yun342173024.iteye.com/blog/1873756在做前端校验的时候,要做日期比较的校验,在js中把字符串转化为日期,一时之间还真不知道在js中怎么把一个 ...

  4. CSS学习笔记——定位position属性的学习

    今天学习之前剩下的一个问题:CSS的position属性.首先归纳出和position相关的问题: position作为一个属性,它一共有哪几个属性值? position常用的属性值有哪几个?分别有什 ...

  5. udhcpc 参数使用说明

    当没有网络的时候,板子一直发送dhcp请求,导致程序不往下执行,解决的办法是把它切换到后台运行,可是如何切换到后台呢,有办法,它自带参数可以实现该功能.如下: udhcpc -b -i eth0 -p ...

  6. 文件写操作--WriteLog

    private static void Write(string sMsg, string fileName) { if (sMsg != "") { try { var dir ...

  7. GTK+中的树状列表构件(GtkTreeView)

    GTK+中的树状列表构件(GtkTreeView) GTK+中的树状列表构件(GtkTreeView) 在本章的GTK+程序设计教程中,我们将向大家重点介绍非常常用也有点复杂的构件--GtkTreeV ...

  8. nyoj 括号匹配

    这个方程有两种形式,本文采用 if(s[i]=s[j]) dp[i][j]=d[i-1][j-1]   dp[i][j]=min(dp[i][k]+dp[k+1][j],dp[i][j]) (i=&l ...

  9. Azure 虚拟机常见问题-上

    在 Azure 虚拟机上可以运行什么? 所有订户均可在 Azure 虚拟机上运行服务器软件.此外,MSDN 订户还可以访问由 Azure 提供的特定 Windows 客户端映像. 就服务器软件来说,你 ...

  10. Storm系列(十一)架构分析之Supervisor-管理Worker进程的事件线程

    处理流程:   方法原型: (defn sync-processes [supervisor]) 函数说明: Supervisor是一个supervisor-data对象. 从local-state中 ...