The Java Debugger (JDB) is a command-line tool that ships with the JDK. JDB is at least conceptually a descendant of the Gnu Debugger (GDB, which was inspired by the original Unix DB), in terms of the instructions for debugging and its command-line interface. JDB is useful for learning about debugging and performing simple debugging tasks, and it's helpful to know it's always available wherever the JDK is installed.
// validating/SimpleDebugging.java
// (c)2017 MindView LLC: see Copyright.txt
// We make no guarantees that this code is fit for any purpose.
// Visit http://OnJava8.com for more book information.
// {ThrowsException}
public class SimpleDebugging {
private static void foo1() {
System.out.println("In foo1");
foo2();
}
private static void foo2() {
System.out.println("In foo2");
foo3();
}
private static void foo3() {
System.out.println("In foo3");
int j = 1;
j--;
int i = 5 / j;
}
public static void main(String[] args) {
foo1();
}
}
/*My Output:
In foo1
In foo2
In foo3
java.lang.ArithmeticException: / by zero
at SimpleDebugging.foo3(SimpleDebugging.java:22)
at SimpleDebugging.foo2(SimpleDebugging.java:15)
at SimpleDebugging.foo1(SimpleDebugging.java:10)
at SimpleDebugging.main(SimpleDebugging.java:26)
*/
Run JDB
$ jdb SimpleDebugging
Initializing jdb ...
> catch Exception
Deferring all Exception.
It will be set after the class is loaded.
> run
run SimpleDebugging
VM start exception: VM initialization failed for: /Library/Java/JavaVirtualMachines/jdk1.8.0_192.jdk/Contents/Home/jre/bin/java -Xdebug -Xrunjdwp:transport=dt_soc
ket,address=*.local:52281,suspend=y SimpleDebugging
ERROR: transport error 202: gethostbyname: unknown host
ERROR: JDWP Transport dt_socket failed to initialize, TRANSPORT_INIT(510)
JDWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugInit.c:750]
The solution is change your hosts file, add 127.0.0.1 *(* in the address=*.local)
$ jdb SimpleDebugging
Initializing jdb ...
> catch Exception
Deferring all Exception.
It will be set after the class is loaded.
> run
run SimpleDebugging
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: In foo1
In foo2
In foo3
Exception occurred: java.lang.ArithmeticException (uncaught)"thread=main", SimpleDebugging.foo3(), line=22 bci=15
22 int i = 5 / j;
main[1]
main[1] list // we can list the point when it hits the excecution stopped in the program source using the list command
18 private static void foo3() {
19 System.out.println("In foo3");
20 int j = 1;
21 j--;
22 => int i = 5 / j;
23 }
24
25 public static void main(String[] args) {
26 foo1();
27 }
main[1]
main[1] locals
Local variable information not available. Compile with -g to generate variable information
main[1] wherei // prints the stack frames pushed in the method stack of the current thread
[1] SimpleDebugging.foo3 (SimpleDebugging.java:22), pc = 15
[2] SimpleDebugging.foo2 (SimpleDebugging.java:15), pc = 8
[3] SimpleDebugging.foo1 (SimpleDebugging.java:10), pc = 8
[4] SimpleDebugging.main (SimpleDebugging.java:26), pc = 0
when run with -g (Generate all debugging info)
$ Javac SimpleDebugging.java -g
main[1] locals
Method arguments:
Local variables:
j = 0
references:
1. On Java 8 - Bruce Eckel
2. https://github.com/wangbingfeng/OnJava8-Examples/blob/master/validating/SimpleDebugging.java
本文介绍如何使用Java Debugger (JDB) 对简单的Java程序进行调试。通过一个示例程序,演示了如何设置断点、查看变量状态及堆栈跟踪等基本调试操作。

被折叠的 条评论
为什么被折叠?



