10 个利用Eclipse调试Java的常见技巧

jopen 12年前
   <h2>1. Conditional Breakpoint</h2>    <p>Hope we know how to add a breakpoint. If not, just click on the left pane (just before the line number) and a breakpoint will be created. In debug perspective, ‘Breakpoints’ view will list the breakpoint created. We can add a boolean condition to it. That is, the breakpoint will be activated and execution will hold only if the boolean condition is met otherwise this breakpoint will be skipped.</p>    <p><img class="alignright size-full wp-image-973" title="conditionaldebug" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/93dd2e18abfa1a537fe3bb8787167ee1.png" width="404" height="290" /></p>    <h2>2. Exception Breakpoint</h2>    <p>In Breakpoints view there is a button labeled as J! We can use that button to add a java exception based breakpoint. For example we want the program to halt and allow to debug when a <a title="Java null and NullPointerException" href="/misc/goto?guid=4958523278294277510">NullPointerException</a> is thrown we can add a breakpoint using this.</p>    <p><img class="alignright size-full wp-image-974" title="javaexceptionbreakpoint" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/33b565ed6abad24ef2459653276aa440.png" width="399" height="318" /></p>    <h2>3. Watch Point</h2>    <p>This is one nice feature I love. When a chosen attribute is accessed or modified program execution will halt and allow to debug. Select a class variable in Outline view and from its context menu select Toggle Watchpoint. This will create a watch point for that attribute and it will be listed in Breakpoints view.</p>    <p><img class="alignright size-full wp-image-975" title="watchpoint" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/4b7c094de53048c3813dc96d879c2a01.png" width="351" height="497" /></p>    <h2>4. Evaluation (Display or Inspect or Watch)</h2>    <p>Ctrl+Shift+d or Ctrl+Shift+i on a selected variable or expression will show the value. We can also add a permanent watch on an expression/variable which will be shown in Expressions view when debug is on.</p>    <p><img class="alignright size-full wp-image-976" title="watch" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/904845bfb1cb1185f18c62bd8de89b8e.png" width="351" height="321" /></p>    <h2>5. Change Variable Values</h2>    <p>We can change the value of a variable on the fly during debug. Choose a variable and go to Variables view and select the value, type and enter.</p>    <p><img class="alignright size-full wp-image-977" title="changevalue" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/22e0fef180178c8d3a4d0087ae1faa3f.png" width="351" height="319" /></p>    <h2>6. Stop in Main</h2>    <p>In Run/Debug Settings, Edit Configuration we can enable a check box that says Stop in main. If enabled when we debug a java program that launches with a main method, the execution halts at first line of main method.</p>    <p><img class="alignright size-full wp-image-978" title="Stopinmain" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/51bca6b32032b59632c492090b67be91.png" width="457" height="433" /></p>    <h2>7. Environment Variables</h2>    <p>Instead of going to System properties to add an environment variable, we can conveniently add it through Edit Configuration dialog box.</p>    <p><img class="alignright size-full wp-image-979" title="environment variable" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/5a87ebdd28160ff5c2435114ece0d056.png" width="451" height="620" /></p>    <h2>8. Drop to Frame</h2>    <p>This is the second best feature I love. We can just return the control to any frame in the call stack during debug. Changes made to variables will not be reset. Choose the stack level which you want to go back and restart debug from there and click the drop to frame button from debug toolbar. Eclipse is cool!</p>    <p><img class="alignright size-full wp-image-980" title="droptoframe" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/a8ffac757e52f8e190a044a3f2d68e4d.png" width="521" height="402" /></p>    <h2>9. Step Filter</h2>    <p>When we Step Into (F5) a method we may go into external libraries (like java) and we may not need it. We can add a filter in preferences and exclude packages.</p>    <p><img class="alignright size-full wp-image-981" title="stepfilter" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/748164b5db43c606ba378c6625a464d9.png" width="566" height="507" /></p>    <h2>10. Step Into, Over and Return</h2>    <p>I kept this as the last point as this is the first thing to learn in debugging :-)</p>    <ul>     <li>F5 – Step Into: moves to next step and if the current line has a method call the control will go into the first line of the called method.</li>     <li>F6 – Step Over: moves the control to next line. If there is a method call in the current line, it executes the method call internally and just moves the control to next line.</li>     <li>F7 – Step Return: When done from inside a method the control will move to the calling line from where the current method is invoked.</li>     <li>F8 – Move to next breakpoint.</li>    </ul>    <p><img class="alignright size-full wp-image-982" title="steps" alt="10 个利用Eclipse调试Java的常见技巧" src="https://simg.open-open.com/show/35772fe99b90b9032307079ebf38864f.png" width="507" height="287" /></p>