javap is a tool that comes with jdk and can be used for code Decompilation You can also view the bytecode generated by the java compiler.
In general, few people use javap to decompile class files, because there are many mature decompilation tools available, such as jad. However, javap can also view the bytecode generated for us by the java compiler. Through it, you can compare the source code and bytecode, so that you can understand the internal work of many compilers.
Example
The javap command decomposes a class file, which decides what to output based on options. If options are not used, javap will output the package, the protected and public fields in the class, and all the methods in the class. Javap will output them to standard output. Let's look at this example. First compile the class below (javac).
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 typing javap DocFooter on the command line, the output is as follows
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 - c, or javap -c DocFooter, is added, the output is as follows
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 output above is bytecode.
Usage Summary
- help Help - l Tables that output rows and variables - public only outputs public methods and fields - Protected only outputs public and protected classes and members - package only outputs packages, public s and protected classes and members, which is the default - p-private outputs all classes and members - s Output Internal Type Signature - c Outputs decomposed code, such as instructions containing java bytecodes in each method of a class. - verbose output stack size, number of method parameters - Constants output static final constants
summary
Javap can be used to decompile and view compiled bytecodes of the compiler. Usually, javap-c is used more. This command is used to list the JVM instructions executed by each method, and to show the actual effect of bytecode of each method. Through the comparison of bytecode and source code, we can deeply analyze the compilation principle of java, and understand and solve various problems of Java principle level.