Java中的Javap工具及示例

2023年 8月 28日 30.6k 0

Java中的Javap工具及示例

javap 工具是检索特定类或接口信息的有用方法。通过其反汇编功能,javap 命令(也称为 Java 反汇编器)被赋予了为用户提供全面的内部信息的作用。从 -c 或 -verbose 这两个选项中,每个选项都会给出独特的结果,用户能够发现自己处于字节码和字节码编排领域。如果不使用任何选项,javap 会运行输入类的 public、protected 和 package 字段和方法。

语法

javap [option] [classname]

登录后复制

不使用选项时

示例

javap class_name

登录后复制

输出

C:UsersAashi>javap java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException; public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait (long) throws java.lang.InterruptedException;
public final void wait(long, int) throws java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
static {};
}

登录后复制

何时使用选项

以下是每个选项以及如何应用它的说明 -

  • -help 或 --help 或 -? -

这用于打印 javap 命令的帮助消息。

示例

javap -help

登录后复制

输出

C:UsersAashi>javap -help
Usage: javap
where possible options include:
-help --help -? Print this usage message
-version Version information
-V -verbose Print additional information
-1 Print line number and local variable tables
-public Show only public classes and members
-protected Show protected/public classes and members
-package Show package/protected/public classes and members (default)
-p -private Show all classes and members
-C Disassemble the code
-S Print internal type signatures
-sysinfo Show system info (path, size, date, MD5 hash) of class being processed
-constants Show final constants
-classpath Specify where to find user class files
-cp Specify where to find user class files
-bootclasspath Override location of bootstrap class files

登录后复制

  • -版本

用于打印Java的版本信息。

示例

javap -version

登录后复制

输出

C:UsersAashi>javap -version
1.8.0_151

登录后复制

  • -v 或 -verbose -

这用于打印附加信息,例如堆栈大小、局部变量数量和方法参数。

示例

javap -v class_name

登录后复制

输出

C:UsersAashi>javap -v java.lang.Object
Classfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/jre/lib/rt.jar! /java/lang/Object.class
Last modified Sep 5, 2017; size 1497 bytes
MD5 checksum 074ebc688a81170b8740f1158648a3c7
Compiled from "Object.java"
public class java.lang.Object
minor version: 0
major version: 52
flags: ACC_PUBLIC, ACC_SUPER
Constant pool:
#1 = Integer 999999
#2 - String #16 //@
#3 = String #38 //nanosecond timeout value out of range
#4 = String #42 //timeout value is negative
#5 - Utf8 ()I
#6 = Utf8 ()Ljava/lang/Object;
#7 - Utf8 ()Ljava/lang/String;
#8 = Utf8 ()V
#9 - Utf8 (I)Ljava/lang/String;
#10 - Utf8 (J)V
#11 = Utf8 (JI)V
#12 - Utf8 (Ljava/lang/Object;)Z
#13 = Utf8 (Ljava/lang/String;)V
#14 - Utf8
#15 = Utf8
#16 - Utf8 @
#17 - Utf8 Code
#18 = Utf8 Exceptions
#19 - Utf8 Line Number Table
#20 = Utf8 Signature
#21- Utf8 Source File
#22 = Utf8 StackMapTable
#23 Utf8 Append
#24 Utf8 Clone
#25 = Utf8 Equals
#26 Utf8 finalize
#27 = Utf8 getClass
#28 Utf8 getName
#29 - Utf8 hashCode
#30 = Utf8 java/lang/Class
#31 - Utf8 java/lang/CloneNotSupportedException
#32 = Utf8 java/lang/IllegalArgumentException
#33 - Utf8 Java/lang/Integer
#34 Utf8 java/lang/InterruptedException

登录后复制

  • -l -

这用于打印行号和局部变量表。

示例

javap -l class_name

登录后复制

输出

C:UsersAashi>javap -1 java.lang.Object
Compiled from "Object.java”
public class java.lang.Object {
public java.lang.Object();
LineNumberTable:
line 37: 0

public final native java.lang.Class getClass();

public native int hashCode();

public boolean equals(java.lang.Object);
LineNumberTable:
line 149: 0

protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;

public java.lang.String toString();
LineNumberTable:
line 236: 0

public final native void notify();

public final native void notifyAll();

public final native void wait(long) throws Java.lang.InterruptedException;
public final void wait(long, int) throws java.lang.InterruptedException;
LineNumberTable:
line 447: 0
line 448: 6
line 451: 16
line 452: 26
line 456: 36
line 457: 40
line 460: 44
line 461: 49

public final void wait() throws java.lang.InterruptedException;
LineNumberTable:
line 502:0
line 503: 5
protected void finalize() throws java.lang.Throwable;
}

登录后复制

  • -公开

这用于仅打印公共类和成员。

示例

javap -public class_name

登录后复制

输出

C:UsersAashi>javap -public java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait (long) throws java.lang.InterruptedException; public final void wait (long, int) throws java.lang.InterruptedException; public final void wait() throws java.lang.InterruptedException;
}

登录后复制

  • -受保护

这用于打印受保护/公共类和成员。

示例

javap -protected class_name

登录后复制

输出

C:UsersAashi>javap -protected java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait(long) throws java.lang.InterruptedException;
public final void wait (long, int) throws java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
}

登录后复制

  • -包

这用于打印包/受保护/公共类和成员(默认)。

示例

javap -package class_name

登录后复制

输出

C:UsersAashi>javap -package java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait (long) throws java.lang.InterruptedException;
public final void wait (long, int) throws java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
static {};
}

登录后复制

  • -c

这用于打印反汇编代码。

示例

javap -c class_name

登录后复制

输出

C:UsersAashi>javap -c java.lang.Object
Compiled from "Object.java"
public class java.lang.Object (public java.lang.Object();
Code:
0: return
public final native java.lang.Class getClass();

public native int hashCode();

public boolean equals(java.lang.Object);
Code:
0: aload e
1: aload_1
2: 1f_acmpne 9
5:iconst_1
6: goto 10
9: iconst_0
10: ireturn

protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;

public java.lang.String toString();
Code:
0: new #52 //class java/lang/StringBuilder
3: dup
4: invokespecial #74 //Method java/lang/StringBuilder."": ()V
7: aload_e
8: invokevirtual #73 // Method getClass: ()Ljava/lang/Class;
11: invokevirtual #67 // Method java/lang/class.getName:()Ljava/lang/String;
14: invokevirtual #76 // Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder;
17: 1dc #2 // String @
19: invokevirtual #76 // Method java/lang/StringBuilder.append: (Ljava/lang/String;)Ljava/lang/StringBuilder;
22: aload_e
23: invokevirtual #78 // Method hashCode: ()I
26: invokestatic #69 // Method java/lang/Integer.toHexString: (I) Ljava/lang/String;
29: invokevirtual #76 // Method java/lang/StringBuilder.append: (Ljava/lang/String;) Ljava/lang/StringBuilder;
32: invokevirtual #75 // Method java/lang/StringBuilder.toString: ()Ljava/lang/String;
35: areturn
public final native void notify();

登录后复制

  • -s -

这用于打印内部类型签名。

示例

javap -s class_name

登录后复制

输出

C:UsersAashi>javap -s java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
descriptor: ()V

public final native java.lang.Class getClass();
descriptor: ()Ljava/lang/class;

public native int hashCode();
descriptor: ()I

public boolean equals(java.lang.Object);
descriptor: (Ljava/lang/Object;)Z

protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
descriptor:()Ljava/lang/Object;

public java.lang.String toString();
descriptor: ()Ljava/lang/String;

public final native void notify();
descriptor: ()V

public final native void notifyAll();
descriptor: ()V

public final native void wait(long) throws java.lang.InterruptedException; descriptor: (J)V

public final void wait(long, int) throws java.lang.InterruptedException; descriptor: (JI)V

public final void wait() throws java.lang.InterruptedException;
descriptor: ()V
protected void finalize() throws java.lang.Throwable;
descriptor: ()V
static {};
descriptor: ()V
}

登录后复制

  • -sysinfo -

这用于打印正在处理的类的系统信息(路径、大小、日期、MD5 哈希)。

示例

javap -sysinfo class_name

登录后复制

输出

C:UsersAashi>javap -sysinfo java.lang.Object
Classfile jar:file:/C:/Program%20Files/Java/jdk1.8.0_151/jre/lib/rt.jar! /java/lang/Object.class
Last modified Sep 5, 2017; size 1497 bytes
MD5 checksum 074ebc688a81170b8740f1158648a3c7
Compiled from "Object.java"
public class java.lang.Object {
public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait (long) throws java.lang.InterruptedException;
public final void wait (long, int) throws java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
static {};
}

登录后复制

  • -常量 -

这用于打印类的最终常量。

示例

javap -constants class_name

登录后复制

输出

C:UsersAashi>javap -constants java.lang.Object
Compiled from "Object.java"
public class java.lang.Object {

public java.lang.Object();
public final native java.lang.Class getClass();
public native int hashCode();
public boolean equals(java.lang.Object);
protected native java.lang.Object clone() throws java.lang.CloneNotSupportedException;
public java.lang.String toString();
public final native void notify();
public final native void notifyAll();
public final native void wait (long) throws java.lang.InterruptedException;
public final void wait(long, int) throws java.lang.InterruptedException;
public final void wait() throws java.lang.InterruptedException;
protected void finalize() throws java.lang.Throwable;
static {};
}

登录后复制

结论

javap 工具对于 Java 编码人员来说是一个多方面且非常强大的工具,使他们能够诊断和调试类文档、评估字段和方法以及从类文档的字节码生成清晰的代码。这个不可或缺的工具适合所有技能水平的 Java 开发人员,使他们能够轻松扩展知识并排除 Java 程序故障。对于任何有抱负的程序员来说,javap 工具都是不可替代的财产,它肯定有助于快速提高他们的编程能力。此外,其广泛的功能可以帮助编码人员完善他们的实践,为他们在构建 Java 应用程序时提供竞争优势。

以上就是Java中的Javap工具及示例的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!

相关文章

JavaScript2024新功能:Object.groupBy、正则表达式v标志
PHP trim 函数对多字节字符的使用和限制
新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
为React 19做准备:WordPress 6.6用户指南
如何删除WordPress中的所有评论

发布评论