jdb调试java程序

jdb是java官方的调试工具,随着jdk一起发布,可以实现基本的调试功能,有了jdb,调试java的时候就告别GDB吧

注意,要顺利调试java程序,java代码编译时必须加上 “-g” 参数

javac -g -sourcepath app/src/main/java/ -d app/src/main/target app/src/main/java/test/App.java

基本用法

jdb的使用格式为:

jdb [ options ] [ class ] [ arguments ]

在实战中发现,使用jdb调试的时候,options最好指定sourcepath, classpath参数,否则可能无法显示代码或者其他错误,sourcepath是代码路径,classpath是依赖库或者class文件路径。

sourcepath, classpath参数的路径要正确,如果是相对路径,就是当前路径开始,一直到包名结束。

假如当前路径为app目录,项目的包是test。目录结构为:

app
└── src
    ├── main
    │   ├── java
    │   │   └── test
    │   │       └── App.java
    │   └── target
    │       └── test
    │           └── App.class

那么source路径就是从当前路径到包test上一级,也就是src/main/java。在app目录下执行:

jdb -sourcepath src/main/java/ -classpath src/main/target test.App

如果依赖库是jar文件,则要指定jar文件的路径,下面指定调试程序的classpath包含appcation-java/lib下面的所有jar包,多个路径之间用冒号分隔,classpath的参数要放在双引号里面

jdb -classpath "application-java/lib/*:src/main/target"  -sourcepath src/main/java/ test.App

注意最后要调试的类必须是“包+类”这样的完整名称,即test.App,不能只是App

启动成功后,jdb会停住,等待设置断点,我们在main方法设置个断点:

stop at test.App.main

然后开始调试:

run

常用调试指令

help                          帮助命令
stop at <class id>.<method>   方法上设置断点
stop at <class id>:<line>     代码行设置断点
run                           运行程序

list        查看代码
next        单步运行
step        进入函数
step up     跳出函数
cont        继续运行到下一个断点

locals      查看当前的所有本地变量
print       查看表达式的值
dump        查看对象的详细信息,例如各个字段的信息

classes                     可以查看已经加载的所有类
class <class id>            查看类信息,包括父类,嵌套类
methods <class id>          查看类的成员函数,包括继承的
fields <class id>           查看类的成员变量

clear                           查看所有断点
clear <class id>:<line>         清除行中断点
clear <class id>.<method>       清除方法中的断点

Ref



《 “jdb调试java程序” 》 有 4 条评论

  1. can you buy cytotec without insurance For dairy products, we only recommended those that had been specifically included in a clinical trial

  2. Glial responses in AD include molecular, morphological, and functional changes in astrocytes and microglia 2, 3 order misoprostone online

  3. I was diagnosed with stage three aggressive breast cancer in July 2017, and have just completed extensive treatments lasting nearly 18 months including several types of chemotherapy priligy and viagra

  4. Just remember that these things take time so don t be discouraged if it takes a few rounds can you buy priligy in usa Biphasic course of disease

回复 can i buy generic cytotec tablets 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注

About Me

一位程序员,会弹吉他,喜欢读诗。
有一颗感恩的心,一位美丽的妻子,两个可爱的女儿
mail: geraldlee0825@gmail.com
github: https://github.com/lisuxiaoqi
medium: https://medium.com/@geraldlee0825