这里是登陆并感兴趣的人的答案。
创建类
import java.util.concurrent.Callable;
public class Clazz {
public static void main(String[] args) throws Exception {
new Clazz().method();
}
public void method() throws Exception {
Clazz.staticMethod(() -> {
Integer x = 1;
Long y = 2L;
y = x * y; // I need a Break point here
return y;
});
}
private static void staticMethod(Callable i) throws Exception {
System.out.println("i = " + i.call());
}
}
编译
javac Clazz.java
为这个类启动 jdb
jdb Clazz
Initializing jdb ...
使用stop在main方法中设置断点
> stop in Clazz.main
Deferring breakpoint Clazz.main.
It will be set after the class is loaded.
使用run启动调试会话
> run
run Clazz
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Clazz.main
Breakpoint hit: "thread=main", Clazz.main(), line=6 bci=0
6 new Clazz().method();
现在调试器在 main 方法中停止,就在调用 new Clazz().method(); 之前。
要找到我们感兴趣的行我们list源
main[1] list
2
3 public class Clazz {
4
5 public static void main(String[] args) throws Exception {
6 => new Clazz().method();
7 }
8
9 public void method() throws Exception {
10 Clazz.staticMethod(() -> {
11 Integer x = 1;
main[1] list 12
8
9 public void method() throws Exception {
10 Clazz.staticMethod(() -> {
11 Integer x = 1;
12 => Long y = 2L;
13 y = x * y; // I need a Break point here
14 return y;
15 });
16 }
17
需要命令list 12 来列出以下行。在输出中我们可以看到我们想要在13 行停止。所以让我们用stop 命令在那里设置一个新断点
main[1] stop at Clazz:13
Set breakpoint Clazz:13
要继续执行直到下一个断点发出命令cont
main[1] cont
>
Breakpoint hit: "thread=main", Clazz.lambda$method$0(), line=13 bci=12
13 y = x * y; // I need a Break point here
我们不在线13,例如可以dumpx 和y 的值。
main[1] dump x
x = {
MIN_VALUE: -2147483648
MAX_VALUE: 2147483647
TYPE: instance of java.lang.Class(reflected class=int, id=568)
digits: instance of char[36] (id=569)
DigitTens: instance of char[100] (id=570)
DigitOnes: instance of char[100] (id=571)
sizeTable: instance of int[10] (id=572)
value: 1
SIZE: 32
BYTES: 4
serialVersionUID: 1360826667806852920
java.lang.Number.serialVersionUID: -8742448824652078965
}
main[1] dump y
y = {
MIN_VALUE: -9223372036854775808
MAX_VALUE: 9223372036854775807
TYPE: instance of java.lang.Class(reflected class=long, id=574)
value: 2
SIZE: 64
BYTES: 8
serialVersionUID: 4290774380558885855
java.lang.Number.serialVersionUID: -8742448824652078965
}
继续step 进一步
main[1] step
>
Step completed: "thread=main", Clazz.lambda$method$0(), line=14 bci=26
14
我们现在可以再次dump y 的值
main[1] dump y
y = {
MIN_VALUE: -9223372036854775808
MAX_VALUE: 9223372036854775807
TYPE: instance of java.lang.Class(reflected class=long, id=574)
value: 2
SIZE: 64
BYTES: 8
serialVersionUID: 4290774380558885855
java.lang.Number.serialVersionUID: -8742448824652078965
}