用javap查看编译版本

我们可以用javap命令查看class文件的编译版本。<wbr style="line-height:25px"><div style="line-height:25px">比如对Worke.class使用javap命令:javap -verbose Worker</div> <div style="line-height:25px">我将看到Worker的反编译结果。其中前几行如下:</div> <div style="line-height:25px"> <div style="line-height:25px"><span style="color:#3366FF; line-height:25px">Compiled from "Worker.java"</span></div> <div style="line-height:25px"><span style="color:#3366FF; line-height:25px">public class com.teleca.robin.Worker extends java.lang.Thread</span></div> <div style="line-height:25px"><span style="color:#3366FF; line-height:25px">SourceFile: "Worker.java"</span></div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">minor version: 0</span> </div> <div style="line-height:25px"> <span style="color:#993300; line-height:25px">major version: 49</span> </div> <div style="line-height:25px"> <span style="color:#3366FF; line-height:25px">Constant pool:</span> </div> </div> <div style="line-height:25px"> <span style="color:#003366; line-height:25px">上面的</span><span style="line-height:25px; color:rgb(153,51,0)">minor version: 0和</span><span style="line-height:25px; color:rgb(153,51,0)">major version: 49</span><span style="color:#003366; line-height:25px">就是编译</span>Worke.class<span style="color:#003366; line-height:25px">时使用的jdk编译版本号。</span> </div> <div style="line-height:25px">但是它并不是我们所熟悉的jdk版本号(比如jdk1.5)。</div> <div style="line-height:25px">不过我们可以把<span style="line-height:21px; font-family:verdana,sans-serif">从 JDK 1.1 到 JDK 1.7 编译器编译出的 class 的默认 minor.major version 汇总下就知道对应关系了。</span> </div> <div style="line-height:25px"> <span style="line-height:21px; font-family:verdana,sans-serif"></span> <table width="700" align="center" border="1" style="line-height:25px; border-collapse:collapse"><tbody style="line-height:25px"> <tr style="line-height:25px"> <td width="20%" style="line-height:25px"><span style="line-height:25px">JDK 编译器版本</span></td> <td width="30%" style="line-height:25px"><span style="line-height:25px">target 参数</span></td> <td width="25%" style="line-height:25px"><span style="line-height:25px">十六进制 minor.major</span></td> <td width="25%" style="line-height:25px"><span style="line-height:25px">十进制 minor.major</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.1.8</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不能带 target 参数</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 03 00 2D</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">45.3</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.2.2</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.1)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 03 00 2D</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">45.3</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.2.2</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.2</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 2E</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">46.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.3.1_19</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.1)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 03 00 2D</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">45.3</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.3.1_19</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.3</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 2F</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">47.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">j2sdk1.4.2_10</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.2)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 2E</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">46.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">j2sdk1.4.2_10</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.4</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 30</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">48.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.5.0_11</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.5)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 31</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">49.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.5.0_11</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.4 -source 1.4</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 30</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">48.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.6.0_01</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.6)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 32</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">50.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.6.0_01</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.5</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 31</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">49.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.6.0_01</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.4 -source 1.4</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 30</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">48.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.7.0</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.6)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 32</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">50.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.7.0</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.7</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 33</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">51.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">jdk1.7.0</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.4 -source 1.4</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 30</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">48.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">Apache Harmony 5.0M3</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">不带(默认为 -target 1.2)</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 2E</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">46.0</span></td> </tr> <tr style="line-height:25px"> <td style="line-height:25px"><span style="color:#003366; line-height:25px">Apache Harmony 5.0M3</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">-target 1.4</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">00 00 00 30</span></td> <td style="line-height:25px"><span style="color:#003366; line-height:25px">48.0<br style="line-height:25px"></span></td> </tr> </tbody></table> <span style="line-height:25px">注1:</span><span style="color:#003366; line-height:25px">javac 有个 -target 参数,编译时可以通过</span><span style="color:#003366; line-height:25px"><span style="line-height:21px; font-family:verdana,sans-serif"><span style="line-height:22px">target</span></span><span style="line-height:21px; font-family:verdana,sans-serif">参数来设置编译时所使用的jdk版本。比如如果目标 JVM 是 1.3 的话,编译选项就可以用 -target 1.3 -source 1.3 了。</span></span> </div> <div style="line-height:25px"><span style="line-height:21px; font-family:verdana,sans-serif"><span style="line-height:21px"><span style="color:#003366; line-height:25px">相应的如果使用 ant ,<span style="line-height:25px; font-family:Arial,Helvetica,sans-serif; color:rgb(0,0,0)"><span style="line-height:21px; font-family:verdana,sans-serif"><span style="color:#003366; line-height:22px">编译时可以通过</span></span><span style="color:#003366; line-height:22px"><span style="line-height:21px; font-family:verdana,sans-serif"><span style="line-height:22px">target</span></span><span style="line-height:21px; font-family:verdana,sans-serif">参数来设置编译时所使用的jdk版本。比如:</span></span></span><br style="line-height:25px"> &lt;javac target="1.4" source="1.4" ............................/&gt;</span></span></span></div> <div style="line-height:25px"> <span style="line-height:21px; font-family:verdana,sans-serif"><span style="line-height:21px"><span style="line-height:25px">注2</span><span style="color:#003366; line-height:25px">:javac中</span></span></span><span style="color:#003366; line-height:25px"><span style="line-height:21px; font-family:verdana,sans-serif">source和</span><span style="line-height:21px; font-family:verdana,sans-serif">target 的意义</span></span> </div> <div style="line-height:25px"><span style="color:#FF6600; line-height:25px"><span style="line-height:21px; font-family:verdana,sans-serif"><span style="line-height:21px"></span></span><span style="line-height:21px; font-family:verdana,sans-serif">-source &lt;版本&gt; 提供与指定版本的源兼容性</span></span></div> <span style="line-height:21px; font-family:verdana,sans-serif"><span style="color:#FF6600; line-height:25px">-target &lt;版本&gt; 生成特定 VM 版本的类文件</span></span> <div style="line-height:25px"><span style="color:#993300; line-height:25px"><span style="line-height:21px; font-family:verdana,sans-serif">source才是真正的指明代码的兼容性,</span><span style="line-height:21px; font-family:verdana,sans-serif">target则是指定了生成class的格式的版本号,即</span><span style="line-height:21px; font-family:verdana,sans-serif">生成特定 VM 版本的类文件。</span></span></div> </wbr>
### 如何使用 `javap -verbose` 反编译 Java 字节码 `javap -verbose` 是一种强大的工具,用于反汇编 `.class` 文件并提供详细的字节码信息。它不仅显示方法和字段的定义,还提供了关于常量池、访问标志和其他元数据的信息。 以下是具体说明: #### 使用方式 运行以下命令即可实现对指定 `.class` 文件的详细反编译: ```bash javap -verbose <ClassName> ``` 其中 `<ClassName>` 表示目标类名或者带有路径的 `.class` 文件名称。 #### 输出解析 当执行上述命令时,会得到非常详尽的结果,主要包括以下几个部分: 1. **头部信息**: 显示版本号以及此 .class 文件是由哪个 JDK 编译而来等内容。 2. **常量池(Constant Pool)**: 列出了所有的字符串文字、类引用、方法引用等静态资源项列表[^4]。 3. **字段详情(Field Information)**: 如果存在成员变量,则这里会有它们各自的描述符及其属性标记。 4. **方法体(Method Bodies)**: 展现各个函数内部逻辑对应的机器级操作序列,即所谓的“字节码”。 下面给出一个具体的实例演示整个流程。 --- #### 实际案例展示 假设有一个简单的小程序如下所示: ```java public class HelloWorld { public static void main(String[] args){ System.out.println("Hello World!"); } } ``` 经过正常编译之后生成了名为 `HelloWorld.class` 的二进制文件。现在我们可以尝试用不同选项调用 `javap` 查看它的结构变化情况。 ##### 步骤一:基本形式 (`javap`) 仅打印公开API接口的部分摘要视图。 ```bash $ javap HelloWorld Compiled from "HelloWorld.java" public class HelloWorld { public HelloWorld(); public static void main(java.lang.String[]); } ``` ##### 步骤二:增加细节程度(`javap -c`) 除了上一步的内容外还会附加上每条语句对应的实际CPU指令集表示法。 ```bash $ javap -c HelloWorld Compiled from "HelloWorld.java" public class HelloWorld { public HelloWorld(); Code: 0: aload_0 1: invokespecial #1 // Method java/lang/Object."<init>":()V 4: return public static void main(java.lang.String[]); Code: 0: getstatic #7 // Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc #13 // String Hello World! 5: invokevirtual #15 // Method java/io/PrintStream.println:(Ljava/lang/String;)V 8: return } ``` ##### 步骤三:完全剖析模式(`javap -v | -verbose`) 最终形态下几乎涵盖了所有可能关心的技术要点,比如但不限于局部变量表(LocalVariableTable)、异常处理器(ExceptionHandler Table)等等... ```bash $ javap -verbose HelloWorld Classfile /path/to/HelloWorld.class Last modified ...; size ... MD5 checksum ... Compiled from "HelloWorld.java" public class HelloWorld extends java.lang.Object{ //省略部分内容... SourceFile: "HelloWorld.java" minor version: 0 major version: 52 flags: ACC_PUBLIC, ACC_SUPER Constant pool: #1 = Methodref #6.#19 // java/lang/Object."<init>":()V #2 = Class #20 // HelloWorld ...(其余大量输出)... } ``` 以上就是利用 `javap -verbose` 来深入探索已有的 Java 类型定义的过程概述。 --- ### 注意事项 虽然借助此类手段能够帮助开发者更好地理解 JVM 底层运作机制原理,但在日常项目维护过程中并不推荐频繁依赖这种方式解决问题;毕竟大多数时候阅读清晰易懂的人类可读源代码要比猜测晦涩难懂的低层次表达更加高效快捷得多! 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值