Source: Hollis (@Hollis_Chuang)
The JAVAP is a tool that comes with the JDK, which can be used to decompile the code or view the bytecode generated by the Java compiler.
In general, few people use JAVAP to decompile class files because there are many mature anti-compilation tools that can be used, such as Jad. However, JAVAP can also view the bytecode generated by the Java compiler for us. It allows you to understand the internal work of many compilers against source code and bytecode.
Instance
The JAVAP command breaks down a class file, which depends on the options to decide what to output. If you do not use options, then JAVAP will output the package, the protected and public fields in the class, and all the methods in the class. javapthey will be output to the standard output. To see this example, first compile ( javac ) the following class.
| 123456789101112131415161718 |
import java.awt.*;import java.applet.*;public class DocFooter extends Applet { String date; String email; public void init() { resize(500,100); date = getParameter("LAST_UPDATED"); email = getParameter("EMAIL"); } public void paint(Graphics g) { g.drawString(date + " by ",100, 15); g.drawString(email,290,15); }} |
After you type JAVAP docfooter on the command line, the output is as follows
| 12345678 |
Compiled from "DocFooter.java"public class DocFooter extends java.applet.Applet { java.lang.String date; java.lang.String email; public DocFooter(); public void init(); public void paint(java.awt.Graphics);} |
If you join the-C, which is javap-c docfooter, the output is as follows
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
Compiled from "DocFooter.java"public class DocFooter extends java.applet.Applet { java.lang.String date; java.lang.String email; public DocFooter(); Code: 0: aload_0 1: invokespecial #1// Method java/applet/Applet."<init>":()V 4: return public void init(); Code: 0: aload_0 1: sipush 500 4: bipush 100 6: invokevirtual #2// Method resize:(II)V 9: aload_0 10: aload_0 11: ldc #3// String LAST_UPDATED 13: invokevirtual #4 // Method getParameter:(Ljava/lang/String;)Ljava/lang/String; 16: putfield #5// Field date:Ljava/lang/String; 19: aload_0 20: aload_0 21: ldc #6// String EMAIL 23: invokevirtual #4 // Method getParameter:(Ljava/lang/String;)Ljava/lang/String; 26: putfield #7// Field email:Ljava/lang/String; 29: return public void paint(java.awt.Graphics); Code: 0: aload_1 1: new#8 // class java/lang/StringBuilder 4: dup 5: invokespecial #9// Method java/lang/StringBuilder."<init>":()V 8: aload_0 9: getfield #5// Field date:Ljava/lang/String; 12: invokevirtual #10 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 15: ldc #11// String by 17: invokevirtual #10 // Method java/lang/StringBuilder.append:(Ljava/lang/String;)Ljava/lang/StringBuilder; 20: invokevirtual #12// Method java/lang/StringBuilder.toString:()Ljava/lang/String; 23: bipush 100 25: bipush 15 27: invokevirtual #13// Method java/awt/Graphics.drawString:(Ljava/lang/String;II)V 30: aload_1 31: aload_0 32: getfield #7// Field email:Ljava/lang/String; 35: sipush 290 38: bipush 15 40: invokevirtual #13// Method java/awt/Graphics.drawString:(Ljava/lang/String;II)V 43: return} |
The above output is byte code.
Usage Summary
| 12345678910 |
-help 帮助-l 输出行和变量的表-public 只输出public方法和域-protected 只输出public和protected类和成员-package 只输出包,public和protected类和成员,这是默认的-p -private 输出所有类和成员-s 输出内部类型签名-c 输出分解后的代码,例如,类中每一个方法内,包含java字节码的指令,-verbose 输出栈大小,方法参数的个数-constants 输出静态final常量 |
Summarize
JAVAP can be used to decompile and view the compiler-compiled bytecode. Usually used javap -c more often, the command is used to list the JVM instructions executed by each method, and shows the actual function of the bytecode of each method. Through the comparison between bytecode and source code, we can deeply analyze the compiler principle of Java, and understand and solve various Java principle level problems.
This series:
- Java Command Learning Series (1): Jps
- Java Command Learning Series (2): Jstack
- Java Command Learning Series (3): Jmap
- Java Command Learning Series (4): Jstat
- Java Command Learning Series (5): Jhat
- Java Command Learning Series (6): Jinfo
- Java Command Learning Series (7): JAVAP
Http://www.importnew.com/18398.html
Java Command Learning Series (7): JAVAP (EXT)