Programming Languages CS 4700
Utah State University
Popular in Course
Popular in ComputerScienence
This 4 page Class Notes was uploaded by Reed Aufderhar on Wednesday October 28, 2015. The Class Notes belongs to CS 4700 at Utah State University taught by Staff in Fall. Since its upload, it has received 7 views. For similar materials see /class/230456/cs-4700-utah-state-university in ComputerScienence at Utah State University.
Reviews for Programming Languages
Report this Material
What is Karma?
Karma is the currency of StudySoup.
You can buy or earn more Karma at anytime and redeem it for class notes, study guides, flashcards, and more!
Date Created: 10/28/15
Java Debugging with Eclipse Learn how to use the debugger Don t be too busy sawing to sharpen the saw Starting the program under the debugger is similar to running it Eclipse provides two options Use the full service Run gtDebug menu selection to use a launch configuration or use the express Run gtDebug As gtJava Application selection if the default options are okay Here you can use the latter Create the following class public class HelloWorld public static void mainString args String msg quotHello World System out printlnm sg forint i 0 ilt5 i Systemoutprintlnquot debug test i Save the le and run the program When you want to run it again you can run it again by pressing CtrlF11 or by clicking Run on the toolbar The console will print out the following Hello world debug test 0 debug test 1 debug test 2 debug test 3 debug test 4 In order to debug something we need to add a breakpoint to the class Add a breakpoint by double clicking in the left margin of the text editor on the line that prints out quotdebug testquot A blue dot will appear in the left margin Press F11 to run HelloWorld again This time since there is a breakpoint Eclipse switches to the Debug perspective The Debug perspective opens and highlights the line of code with the breakpoint Another way of starting to debug is to make sure the source for HelloWorld is selected in the editor and select Run gtDebug As gtJava Application from the main menu Eclipse will start the program change to the Debug perspective and suspend execution at the breakpoint The best way of starting the debugger especially when you have multiple projects is to rig ht click on the java file that contains the main program and select Run As or Debug As Iappeared to have less trouble with it running the wrong project that way The Debug perspective includes several new views that are especially useful for debugging First at top left is the Debug view not to be confused with the Debug perspective to which it belongs which shows the call stack and status of all current threads including any threads that have already run to completion Your program which Eclipse started has hit a breakpoint and its status is shown as Suspended m r a Warmly puhllc smug vnld man3rng args t Scrlng msg quotHello Worldquot 3y cemoucpr1nclnlmsgl inuan 1 u x5 n1lt systemou cpnnunlquotdehug 25 quot gt Figure 1 The script will stop running and highlight a bredpoint Next step into the code by pressing F5 or Run gt Step Into a few times As you step through the code the console Window begins to display the results of the Systemout commands You can also watch the variable i in the variables Window on the top right of the Debug perspective or by hovering your cursor over i in the code x Variables i greakpuintsl v args StringU idll u i Figure 2 The variables window updates as the code is mnning TIP To resume running the code but stopping at the breakpoint press F8 or Run gt Resume Double click the breakpoint to remove i As your Eclipse projects become more complex the debugger Will be an invaluable tool for xing bugs Stepping through code In the title bar of the Debug view is a toolbar that lets you control the program39s execution The first few tool buttons which resemble the familiar controls of electronic devices such as CD players allow you to resume suspend or terminate the program Several buttons incorporate arrows in their design these allow you to step through a program a line at a time Holding the mouse over each button in turn will cause tool tips to appear identifying them For example click the second step button Step Into Doing so executes the line of code that is currently highlighted in the editor area below the Debug view Step Into as the name suggests takes you into the method that is called After clicking Step Into the highlighted line is the first executable The Use Step Filters button works the same as Step Into but it39s selective about what methods it will step into You normally want to step only into methods in your own classes and not into the standard Java packages or third party packages You can specify which methods Step Filter will execute and return from immediately by selecting WindowgtPreferencesgtJavagtDebuggtStep Filtering and defining step lters by checking the packages and classes listed there Taking a moment to set these filters is well worth the trouble because Use Step Filters saves you from getting lost deep in unknown code something that can happen all too often when you use Step Into Evaluating variables and expressions To the right of the Debug view is a tabbed notebook containing views that let you examine and modify variables and breakpoints Select the Variables tab if it isn39t already selected This view shows the variables in the current scope and their values Sometimes a program has many variables but you39re interested in only one or a few To watch select variables or expressions you can add them to the watch list in the Expression view To do this select a variable i for instance by double clicking on it in the editor and then rig ht clicking on the selection and choosing Watch from the context menu The variable and its value if it39s in scope will appear in the Expressions view One significant advantage of watching variables in the Variables and Expressions views over using print statements for debugging is that you can inspect objects and their fields in detail and change their values even normally immutable strings Return to the Variables view and expand the msg variable to show its attributes One of these is a Char array value which can be expanded to reveal the individual characters in the msg String For example right click on the character H and select Change Value You will be prompted to enter a new value such as J Below the editor area is another tabbed notebook which includes a Console view If the display tab is not there enter Window gtShow View gtDisplay It allows you to enter any variables that are in scope or arbitrary expressions including these variables Select Display view and enter the following for example msg charAt 3 To immediately evaluate this expression you must first select it and then click the second Display view tool button Display Result of Evaluating Selected Text which displays the results in the Display view It39s usually better to click the first tool button Inspect Result of Evaluating Selected Text because it adds the expression to the Expressions view Either way the value displayed is not automatically updated as the variables in the expression change but in the Expressions view you have the option of converting the expression into a watch expression which is updated as you step through the code To do this change to the Expressions view Notice that the Inspect icon a magnifying glass appears next to the expression Click on the expression and select Convert to Watch Expression from the context menu The icon next to the expression will change to the Watch icon wodyigw 7 M91 manila mm class public smug vnld man3rng argsl lt Scrlng msg quotHello Worldquot 3y cemoucprnclnlmsg mum 1 u x5 1 Systemcutprancln gt Let39s go back to stepping through the code You previously left the cursor at the call s stemoutprlntln If you want to see the code for Systemoutprlntln you can click Step Into otherwise click Step Over to execute the Systemoutprlntln method and start the next iteration of the for loop The tabbed notebook below the editor includes a Console view Program output appears here if you made the earlier change to the variable msg the line quotJello worldquot will appear You can either continue to click Step Over until the loop terminates or if you find this process tedious click Step Return to immediately finish executing the say method and return to the mam method Or just click the Resume button to let the program run to the end Conclusion In order to get out of the debugger do two things Hit the red square to terminate the program Click on the java tab in the upper right hand corner of the big window One of Eclipse39s most useful features is its integrated debugger The ability to execute code interactively setting breakpoints executing code line by line and inspecting the value of variables and expressions is powerful tool for investigating and fixing problems with the code
Are you sure you want to buy this material for
You're already Subscribed!
Looks like you've already subscribed to StudySoup, you won't need to purchase another subscription to get this material. To access this material simply click 'View Full Document'