• 欢迎光临~

how to debug in java

开发技术 开发技术 2022-10-30 次浏览

jdb normal usages

1.compile period

javac -g, add complie information

mikeli@dell-pc:~/code/algo_java$ javac -g DemToBin3.java 

 

2.debug class in one window

jdb <className>

set breakpoint:

1:to method : stop in <class name>:<method name>

2:to line : stop at <calss name>.<line number>

run className

 

example 1:

mikeli@dell-pc:~/code/algo_java$ jdb DemToBin3
Initializing jdb ...
> stop in DemToBin3.main
Deferring breakpoint DemToBin3.main.
It will be set after the class is loaded.
> run DemToBin3
run  DemToBin3
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable

VM Started: Set deferred breakpoint DemToBin3.main

 

example 2:

mikeli@dell-pc:~/code/algo_java$ jdb DemToBin3
Initializing jdb ...
> stop at DemToBin3:5
Deferring breakpoint DemToBin3:5.
It will be set after the class is loaded.
> run DemToBin3
run  DemToBin3
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable

VM Started: Set deferred breakpoint DemToBin3:5

 

3.debug another class in other window

for example , A class is running in another window,or remote environment

we compile A class like below:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y Acalss

jvm will show the running port(such as :8008)

then we use this port to debug:

jdb -attach 8008

 

 

4.debug common usage

execute usage: step  and cont

show parameter information: locals,print

show executing code:list

main[1] step

Step completed: "thread=main", DemToBin3.main(), line=6 bci=3
6            String rs="";

main[1] step

Step completed: "thread=main", DemToBin3.main(), line=13 bci=6
13                if(x!=0)

 

cont   Continues execution of the debugged application after a breakpoint, exception, or step.

VM Started: Set deferred breakpoint DemToBin3.main

Breakpoint hit: "thread=main", DemToBin3.main(), line=5 bci=0
5            int x=10;

main[1] cont     
> 1010

 

 

              The print command supports many simple Java expressions including those with method invocations, for example:

              print MyClass.myStaticField
              print myObj.myInstanceField
              print i + j + k (i, j, k are primities and either fields or local variables)
              print myObj.myMethod() (if myMethod returns a non-null)
              print new java.lang.String("Hello").length()

 

 

main[1] list
1    public class DemToBin3
2    {
3        public static void main(String[] args)
4        {
5 =>         int x=10;
6            String rs="";
7    
8            //loop,until x=0
9            //if x!=0, x%2 as mod,link to result.
10            //then x/2,into next loop

 

 

attach source code:

like java option

jdb -sourcepath dir1:dir2:dir3 ...

such as      jdb -sourcepath  .:/bin:/

程序员灯塔
转载请注明原文链接:how to debug in java
喜欢 (0)
违法和不良信息举报电话:022-22558618 举报邮箱:dljd@tidljd.com