📄 ch07.html
字号:
<EM CLASS="C-Code">SimpleJdbTest</EM><EM CLASS="Bold">:</EM></H6><PRE CLASS="CODE"><A NAME="pgfId-1087629"></A>javac -g SimpleJdbTest.java</PRE></DIV><DIV><H6 CLASS="D"><A NAME="pgfId-1087630"></A><EM CLASS="Bold">Start the </EM><EM CLASS="C-Code">jdb</EM><EM CLASS="Bold"> tool:</EM></H6><P CLASS="Body"><A NAME="pgfId-1087632"></A><A NAME="marker-1087631"></A>Next, start the <EM CLASS="CODE">jdb</EM> tool with the program class name as a parameter: </P><PRE CLASS="CODE"><A NAME="pgfId-1087633"></A>jdb SimpleJdbTestInitializing jdb...0xad:class(SimpleJdbTest)</PRE><P CLASS="Body"><A NAME="pgfId-1087634"></A>To debug an applet in appletviewer use the <EM CLASS="CODE">-debug</EM> parameter as in this example: </P><PRE CLASS="CODE"><A NAME="pgfId-1087635"></A>$ appletviewer -debug MyApplet.htmlInitializing jdb...0xee2f9808:class(sun.applet.AppletViewer)> </PRE><P CLASS="Body"><A NAME="pgfId-1087638"></A><EM CLASS="Bold">Setting a Breakpoint and Listing Methods. </EM><A NAME="marker-1087636"></A><A NAME="marker-1087637"></A>At this point, the <EM CLASS="CODE">SimpleJdbTest</EM> class has only been loaded; the class constructor has not been called. To make <EM CLASS="CODE">jdb</EM> stop when the program is first instantiated, put a stop, or breakpoint, at the constructor using the <EM CLASS="CODE">stop in</EM> command. When the breakpoints have been set, instruct <EM CLASS="CODE">jdb</EM> to run your program using the <EM CLASS="CODE">run</EM> command as follows: </P><PRE CLASS="CODE"><A NAME="pgfId-1087639"></A>> stop in SimpleJdbTest.<init>Breakpoint set in SimpleJdbTest.<init>runrun SimpleJdbTestrunning ...main[1]Breakpoint hit: SimpleJdbTest.<init> (SimpleJdbTest:10)</PRE><P CLASS="Body"><A NAME="pgfId-1087641"></A><A NAME="marker-1087640"></A>The <EM CLASS="CODE">jdb</EM> tool stops at the first line in the constructor. To list the methods that were called to get to this breakpoint, enter the <EM CLASS="CODE">where</EM> command: </P><PRE CLASS="CODE"><A NAME="pgfId-1087642"></A>main[1] where[1] SimpleJdbTest.<init> (SimpleJdbTest:10)[2] SimpleJdbTest.main (SimpleJdbTest:29)</PRE><P CLASS="Body"><A NAME="pgfId-1087643"></A>The numbered method in the list is the last stack frame that the Java virtual machine has reached. In this case the last stack frame is the <EM CLASS="CODE">SimpleJdbTest</EM> constructor that was called from <EM CLASS="CODE">SimpleJdbTest</EM> main. </P><P CLASS="Body"><A NAME="pgfId-1087645"></A><A NAME="marker-1087644"></A>Whenever a new method is called, it is placed on this stack list. Java Hotspot technology achieves some of its speed gains by eliminating a new stack frame when a new method is called. </P><P CLASS="Body"><A NAME="pgfId-1087646"></A>To get a general appreciation of where the code has stopped, enter the <EM CLASS="CODE">list</EM> command. </P><PRE CLASS="CODE"><A NAME="pgfId-1087647"></A>main[1] list6 Panel p;7 Button b;8 int counter=0;910 SimpleJdbTest() {11 setSize(100,200);12 setup();13 }14 void setup (){</PRE><P CLASS="Body"><A NAME="pgfId-1087649"></A><EM CLASS="Bold">Locating the Source. </EM><A NAME="marker-1087648"></A>If the source to the class file stopped in is not available on the current path, you can tell <EM CLASS="CODE">jdb</EM> to find the source with the <EM CLASS="CODE">use</EM> command by giving it the source directory as a parameter. In the following example, the source is in a subdirectory or folder called <EM CLASS="CODE">book</EM>. </P><PRE CLASS="CODE"><A NAME="pgfId-1087650"></A>main[1] listUnable to find SimpleJdbTest.javamain[1] use bookmain[1] list6 Panel p;7 Button b[];8 int counter=0;9 10 => SimpleJdbTest() {</PRE><P CLASS="Body"><A NAME="pgfId-1087652"></A><EM CLASS="Bold">Looking at a Method. </EM><A NAME="marker-1087651"></A>To see what happens in the <EM CLASS="CODE">setup</EM> method for <EM CLASS="CODE">SimpleJdbText</EM>, use the <EM CLASS="CODE">step</EM> command to step through the four lines to get to it. </P><PRE CLASS="CODE"><A NAME="pgfId-1087653"></A>main[1] stepmain[1]Breakpoint hit: java.awt.Frame.<init> (Frame:222)</PRE><P CLASS="Body"><A NAME="pgfId-1087654"></A>This is now the <EM CLASS="CODE">Frame</EM> class constructor! If you keep stepping you follow the <EM CLASS="CODE">Frame</EM> constructor and not the <EM CLASS="CODE">SimpleJdbText</EM> class. Because <EM CLASS="CODE">SimpleJdbTest</EM> extends the <EM CLASS="CODE">Frame</EM> class, the parent constructor, which in this case is <EM CLASS="CODE">Frame</EM>, is called on your behalf. </P><P CLASS="Body"><A NAME="pgfId-1087656"></A><EM CLASS="Bold">The </EM><EM CLASS="C-Code">step</EM> <EM CLASS="C-Code">up</EM><EM CLASS="Bold"> Command. </EM><A NAME="marker-1087655"></A>You could continue stepping and eventually you will return to the <EM CLASS="CODE">SimpleJdbTest</EM> constructor, but to return immediately, you can use the <EM CLASS="CODE">step up</EM> command to go back to the <EM CLASS="CODE">SimpleJdbTest</EM> constructor. </P><PRE CLASS="CODE"><A NAME="pgfId-1087657"></A>main[1] step upmain[1]Breakpoint hit: SimpleJdbTest.<init> (SimpleJdbTest:8)</PRE><P CLASS="Body"><A NAME="pgfId-1087659"></A><EM CLASS="Bold">The </EM><EM CLASS="C-Code">next</EM><EM CLASS="Bold"> Command. </EM><A NAME="marker-1087658"></A>You can also use the <EM CLASS="CODE">next</EM> command to get to the <EM CLASS="CODE">setup</EM> method. In this next example, the <EM CLASS="CODE">jdb</EM> tool has approximated that the source line is outside the constructor when it processed the last <EM CLASS="CODE">step up</EM> command. To return to the constructor, use another <EM CLASS="CODE">step</EM> command, and to get to the <EM CLASS="CODE">setup</EM> method, use a <EM CLASS="CODE">next</EM> command. To debug the <EM CLASS="CODE">setup</EM> method, you can step through the <EM CLASS="CODE">setup</EM> method. </P><PRE CLASS="CODE"><A NAME="pgfId-1087660"></A>main[1] stepBreakpoint hit: SimpleJdbTest.<init> (SimpleJdbTest:11)main[1] list7 Button b[]=new Button[2];8 int counter=0;910 SimpleJdbTest() {11 setSize(100,200);<12 setup();13 }14 void setup (){15 p=new Panel();16 }main[1] nextBreakpoint hit: SimpleJdbTest.<init> (SimpleJdbTest:12)main[1] stepBreakpoint hit: SimpleJdbTest.setup (SimpleJdbTest:15)</PRE><P CLASS="Body"><A NAME="pgfId-1087662"></A><EM CLASS="Bold">The </EM><EM CLASS="C-Code">list</EM><EM CLASS="Bold"> and </EM><EM CLASS="C-Code">print</EM><EM CLASS="Bold"> Commands. </EM><A NAME="marker-1087661"></A>Another way to get to the <EM CLASS="CODE">setup</EM> method is to use the <EM CLASS="CODE">stop in SimpleJdbTest.setup</EM> command. Before using <EM CLASS="CODE">stop in</EM>, list the source to check where you are: </P><PRE CLASS="CODE"><A NAME="pgfId-1087663"></A>main[1] list11 setSize(100,200);12 setup();13 }14 void setup (){15 => p=new Panel();16 b[0]= new Button("press");17 p.add(b[0]);18 add(p);19</PRE><P CLASS="Body"><A NAME="pgfId-1087665"></A><A NAME="marker-1087664"></A>The first thing the <EM CLASS="CODE">setup</EM> method does is create a <EM CLASS="CODE">Panel p</EM>. If you try to display the value of <EM CLASS="CODE">p</EM> with the <EM CLASS="CODE">print p</EM> command, you will find that the value is null. </P><PRE CLASS="CODE"><A NAME="pgfId-1087666"></A>main[1] print pp = null</PRE><P CLASS="Body"><A NAME="pgfId-1087667"></A>This occurred because the line has not been executed and so field <EM CLASS="CODE">p</EM> has not been assigned a value. You need to step over that assignment operation with the <EM CLASS="CODE">next </EM>command and then use the <EM CLASS="CODE">print p</EM> command again. </P><PRE CLASS="CODE"><A NAME="pgfId-1087668"></A>main[1] next </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087669"></A>Breakpoint hit: SimpleJdbTest.setup (SimpleJdbTest:16) </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087670"></A>main[1] print p</PRE><PRE CLASS="CODE"><A NAME="pgfId-1087671"></A>p = java.awt.Panel[panel0,0,0,0x0,invalid, layout=java.awt.FlowLayout]</PRE><P CLASS="Body"><A NAME="pgfId-1087673"></A><EM CLASS="Bold">Using the </EM><EM CLASS="C-Code">stop in</EM><EM CLASS="Bold"> Command to Set Breakpoints on Overloaded Methods. </EM><A NAME="marker-1087672"></A>Although stepping through small classes is fast, as a general rule on larger applications, it is often a lot faster to set breakpoints. This is partly because <EM CLASS="CODE">jdb</EM> has a very simple command set and no shortcuts, so each command has to be pasted or typed in full. </P><P CLASS="Body"><A NAME="pgfId-1087674"></A>To set a breakpoint in the <EM CLASS="CODE">Button</EM> class, use <EM CLASS="CODE">stop in java.awt.Button.<init></EM>:</P><PRE CLASS="CODE"><A NAME="pgfId-1087675"></A>main[1] stop in java.awt.Button.<init> </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087676"></A>java.awt.Button.<init> is overloaded,use one of the following: </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087677"></A>void <init> </PRE><PRE CLASS="CODE"><A NAME="pgfId-1087678"></A>void <init>(java.lang.String)</PRE><P CLASS="Body"><A NAME="pgfId-1087679"></A>The message explains why <EM CLASS="CODE">jdb</EM> cannot stop in this method without more information, but the message is slightly misleading because you do not need to specify the return type for overloaded methods, you just need to be explicit about exactly which one of the overloaded methods you want to <EM CLASS="CODE">stop in</EM>. To <EM CLASS="CODE">stop in </EM>the constructor that creates this <EM CLASS="CODE">
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -