Java Programming – How To Use The Eclipse Debugger Tools
Video Title: Java Programming – How To Use The Eclipse Debugger Tools
In this video I’m going to show you how to use the Eclipse Java debugger and I’m gonna highlight three important things that you’re going to need to know to run your program in debug mode the first of which is switching between perspectives a perspective is simply the arrangement
Of views that you see when you open up Eclipse these views you might think of these as panels or windows and the default view which is the job of view has a certain arrangement of views and the debug view or perspective is going to have an arrangement of views
That is different and it’s more helpful for the debugging purposes the second thing I’m going to focus on is breakpoints and how to step through your code at runtime and the third thing I’m going to finish up the video with is some optional things that you can do with breakpoints and
That is add a hit counter or conditional to it so I’m gonna be demonstrating this in Eclipse neon which is the latest version as the time of this recording and I’ve made a sample program that has two of what I consider to be the most likely reasons you would need to step through
Some code you either have a loop that you want to examine and see how it’s processing at runtime or you have a nested block of code that you want to see how it goes through this nested block so essentially I have a real short program here that I called debug me and we’re
Gonna watch two variables change over time one that I call iteration and that’s just gonna let us know how many steps have taken place another one called mystery so we’re watching over a variable called mystery to see how its value changes when the program is running okay so let’s first talk about perspectives
The way that you see these panels over here or everywhere in if I were to mess this up a little bit this is called the perspective and you can arrange this however you want whatever is the most comfortable for you anytime you want to reset this you would go to the window menu
Scroll down to perspective and say reset perspective and I’m going to confirm that by clicking yes and it’s gonna pop back to its default so this is the Java perspective if we want to get to the debug perspective there’s a couple of ways we could also go to this window
Menu go down to perspective open perspective and select the debug option that’s going to switch us into this arrangement of panels which now we have a couple of new ones we have a debug panel up here they’re known as views and eclipse a variables view a breakpoints view as
Well an outline over here this is our code itself the console shifted to the bottom you also have a tasks view open as well and now if I wanted to get back to my original perspective again I have multiple ways to do this but I can go to window
Scroll down to perspective open perspective and now switch back to Java and now I’ve returned to where I’ve come from a quicker way to do that is you can toggle between these two views over here once the once you’ve launched them so the default one is Java and then
The debug one looks like a little bug icon over here you can add additional views by clicking that icon right there and usually the way that I do it is I have my code open and then I’m looking for the debug icon here and this is actually going to run my program in
Debug mode and switch to the debug perspective at the same time so if I go ahead and do that I’m going to go ahead and click this the first time you launch it it’s gonna ask you if you want to switch to that perspective and you want to do that
Every time I usually keep this box checked right here that’ll be an option for you so you can check it you can not check it but I tend to keep it checked but you’re gonna have to click yes to jump to that perspective anyway and now you’re in debug mode the program has
Actually gone through all of its execution and it has printed to the console this big number now if we want to pause the program at some point the way that we can do that is to add breakpoints so a breakpoint what that means is it’s essentially going to pause the program
On that line of execution if and when the program gets there so let me put the definition up here basically alright so if the program can reach that point of code it will pause let me just demonstrate it real quick so here’s our main method we know that it’s at least
Going to get to this line of code so if I double click the line number or to the left of the line number in here what’s called the gutter if I double click there that adds a blue dot that is a breakpoint now if I run the program
Again and I’m going to click the little bug icon to do that it pauses the program at this line of execution which is essentially the first thing it’s going to do so now I have some additional options that I can do so I want to look at the
Tool bar up here and we’re going to go through all of this right here now the first thing I could do is I could now resume the execution of the program by just pushing play and it’s going to run through the rest of the program and spit
Out that number so at any point I want to continue the execution I could push that play button now let me do that again but I’m going to add a second breakpoint so if I double click right here and I push the debug icon in the toolbar
It pauses at this first breakpoint and when I push resume and will go all the way to the next breakpoint so sometimes you want to add multiple breakpoints in here and jump from one to the next and a quick way to do that is by arranging
Them like like so all right if I want to stop the program this is the terminate button and sometimes after you terminate I like to clear out my debug window up here so I don’t have any additional threads running let me go ahead and remove these break
Points right now so I’m going to double click this one and double click that one and that gets rid of them and we’re gonna put them at a different point so let’s put one on line 14 right now where this variable called mystery is going to get a value from this method called
Calculate so let’s go ahead and run the program again and now I have the option right here in the toolbar to step into or step over let me show you the difference between the two if I step into it’s going to go into the calculate method so that means that it’s
Going to jump from this line so the green line here is the line that it’s currently on and it has not executed that line yet so it’s going to jump from this line into this method here called calculate and that’s if I did the step into option
If I did step over it would not run this or basically it’s going to run this line but then go right to line 15 so if we’re interested in knowing what the calculate method is going to return so and store in mystery I might want to step into it
If I’m not interested in it I might want to step over it so I’m going to go ahead and click step into and you’re gonna see it jumped from line 14 down to line 26 now if I look at the tool bar I have three options and that’s because
Essentially they’re the first two options step into and step over are going to function exactly the same so if I step into it just calculated this method here step over what I’ve done the same thing and now I’m on return to mystery the third option is called step return step
Return is going to take us back to where we came from so we came from this the main method into the calculate method if I click step return right now it’s gonna take me back now because this is the end of the method down here essentially all
Three of these are going to be doing the same thing they would all take us back to where we came from so I’m gonna go ahead and click this third option which takes us back to line 14 but now calculate this has become an expression that can now be evaluated we know what
The result of it is going to be and when we go to the next line it’s going to essentially give that result to the variable mystery to hold so I’m going to go ahead and click Next and now mystery is listed right here in our variable list with its value
Now I can continue and now it’s going to go down into this nested structure so another thing that you may want to use in debug mode is you’ve got a complicated nested condition and you want to find out how exactly it’s working at certain points of the
Program’s so this allows us to step through that as well if I click step into it’s going to go down to the conditions that are true and return the result and now we’ve gone through one iteration of this loop all right so we’ve gone through the loop one time and
Mystery is zero if I step through this a couple of times I’m gonna watch this variable list up here change its value so iteration at this point in the program we’ve gone one time mystery is now got a value of four and after a couple of steps through this
Program I can watch the variables change okay I’m going to click the resume button takes us back all the way up to the top and now on the third iteration mystery is holding a value of 20 now this loop is going to iterate until iteration is less than 100 or no longer
Less than 100 so instead of having to step a whole bunch of times or click this button a whole bunch of times I can set conditions or a hit counter to say run this all the way to the 99th iteration and then pause the execution to do that you’re going to right-click
Or control click on a Mac the breakpoint and go down to breakpoint properties and this allows you to add for instance a hit counter a conditional or both so the first thing I’m going to do is let’s just focus on the hit counter right now let’s say I want to pause the
Program when this breakpoint has been reached 99 times so I’m going to click OK and now I’m going to stop the execution of our previous debug session I’m going to clear that out and I’m gonna run it again but now this breakpoints only going to be triggered after it’s been
Reached 99 times so I’m going to go ahead and click debug and here you can see it went from 0 to 98 that’s 99 iterations and the value of mystery is this big enormous number right here so that’s a quick way to jump through iterations is to add a hit counter but
Maybe you want to do a conditional instead so let me show you quickly how a conditional would work I’m going to go ahead and stop the execution and terminate that thread and I’m going to right click on this breakpoint and go down to breakpoint properties and
Instead of a hit counter I’m going to add a conditional in this case maybe I want to pause when mr. e is greater than I don’t know 10,000 so I’m going to click OK and whenever mr. e becomes greater than 10,000 that’s when it’s going to pause so I’m going to go
Ahead and click a debug and now it’s telling me on the 10th iteration that’s when mystery is holding a value of 16550 so it met that condition and that’s another way to pause the execution okay so now we could combine those two we could say if the hit counter is greater than
Let’s say 20 and the value of mystery is greater than 20,000 I’m going to go ahead and click OK so now we have the situation where the breakpoint here has been reached 20 times I know that because if I look at my iteration variable I went from 0 to
19 which is 20 iterations but the second thing that it has to reach is mystery also has to be a value of over 20,000 so these two conditions are true right now and therefore the execution of the program has paused so it’s both 20 iterations and mystery has a value over 20,000
So you can combine those two things to reach unique situations that you might need to test in debug mode so now let’s do a quick review of everything we’ve talked about in this video I’m gonna go ahead and stop the execution of debug mode clear this out and I’m going to run beginning
And mainly you’re going to be working with the step functions so I could click the step into or step over on this line of code which is not executed yet if I click step into it’s going to have to jump to the calculate method and it will
Step you through that portion but if I wanted to skip that and I just wanted to go from line 14 to line 15 I would want to click the step over option so if I click that it’s going to go to line 15 as opposed to clicking step into which
Now on line 15 would jump us to the get nested result method if I click that it will jump to that method so it can step you through that portion but step return will show up when you are jumping into a method and can come back so if I go
Ahead and click step return it takes us back to the line that called get nested result and some lines of code for instance if I go to the next line like iteration well function pretty much the same whether you click step into or step over because it’s not calling any other
Functions or methods I could always click the resume button to essentially finish the execution of the program the stop button or terminate button is located here and to get back from debug mode to the default perspective you could go to window perspectives open perspective and click
Java and in the top right corner of the perspective that you’re currently in it will say Java up there and you can click that as well so that’s debugging in Eclipse if you have any questions just email me or see me in class
-
Sale!
Wireless WIFI Repeater Extender Amplifier Booster 300Mbps
$29.99$14.99 Add to cartWireless WIFI Repeater Extender Amplifier Booster 300Mbps
Categories: Electronics, Wi-Fi Router, Wireless Wi-Fi Extender Tags: 300Mbps, 802.11N, Amplifier, Booster, Extender, mobile wi-fi booster, Remote, WIFI, Wireless, Wireless WIFI, Wireless WIFI Repeater, Wireless WIFI Repeater Extender, Wireless WIFI Repeater Extender Amplifier, Wireless WIFI Repeater Extender Amplifier Booster, Wireless WIFI Repeater Extender Amplifier Booster 300Mbps$29.99$14.99 -
Sale!
Full RGB Light Design Gaming Headset Headphones with Mic
$24.99$14.99 Add to cartFull RGB Light Design Gaming Headset Headphones with Mic
Categories: Electronics, Gaming, Gaming Headsets Tags: Design, Full, Full RGB Light Design Gaming Headset, Full RGB Light Design Gaming Headset Headphones, Full RGB Light Design Gaming Headset Headphones with Mic, Gamer, Gaming, Gaming Headset Headphones, gaming headset wireless, Headphone, Headphones, Headset, Light, Mic, Package, RGB$24.99$14.99 -
Sale!
Wireless BlueTooth Multi-Device Keyboard Mouse Combo
$39.99$19.99 Add to cartWireless BlueTooth Multi-Device Keyboard Mouse Combo
Categories: Electronics, Gaming, Gaming Keyboards, Keyboard Mouse Combos Tags: Combo, Keyboard, keyboard mouse combos, Mouse, MultiDevice, Set, WireKeyboard Mouse Combo, Wireless, Wireless BlueTooth Keyboard Mouse Combo, Wireless BlueTooth Keyboard Mouse Combos, Wireless BlueTooth Multi-Device Keyboard Mouse Combo, Wireless BlueTooth Multi-Device Keyboard Mouse Combos$39.99$19.99 -
Sale!
High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
$199.99$139.99 Add to cartHigh Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar
Categories: Gaming, Gaming Chairs Tags: Adjustable, Chair, computer chairs, Desk, Executive, Gaming, Girl, Headrest, High, High Back Leather Executive Adjustable Swivel Gaming Chair, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest, High Back Leather Executive Adjustable Swivel Gaming Chair with Headrest and Lumbar, High Back Leather Executive Adjustable Swivel Gaming Chairs, Leather, Lumbar, Office, Racing, Swivel$199.99$139.99 -
Sale!
Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
$29.99$19.99 Select optionsProfessional LED Light Wired Gaming Headphones with Noise Cancelling Microphone
SKU: N/A Categories: Electronics, Gaming, Gaming Headsets Tags: Cancelling, Gaming, Gaming Headphones with Noise Cancelling Microphone, gaming headset, Headphones, Headset, LED, Light, Mic, Microphone, Noise, Professional, Professional LED Light Wired Gaming Headphones, Professional LED Light Wired Gaming Headphones with Noise Cancelling Microphone, Wired, Wired Gaming Headphones, Wired Gaming Headphones with Noise Cancelling Microphone$29.99$19.99 -
Sale!
Gaming Desk with LED Lights USB Power Outlets and Charging Ports
$349.99$249.99 Select optionsGaming Desk with LED Lights USB Power Outlets and Charging Ports
SKU: N/A Categories: Computer Desk, Gaming, Gaming Desk Tags: and Charging Ports, Charging, Desk, Desks, Gaming, gaming desk with led lights, Gaming Desks with LED Lights, Home, LED, Lights, Monitor, Office, Outlets, Port, Power, Room, Stand, USB, USB Power Outlets, White, Workstation$349.99$249.99 -
Sale!
Wired Mixed Backlit Anti-Ghosting Gaming Keyboard
$99.99$79.99 Add to cartWired Mixed Backlit Anti-Ghosting Gaming Keyboard
Categories: Electronics, Gaming, Gaming Keyboards Tags: Antighosting, Backlit, Blue, brown, Gaming, Gaming Keyboard, gaming keyboards, gaming keyboards and mouse, Keyboard, Laptop, Switch, Wired, Wired Mixed Backlit Anti-Ghosting Gaming Keyboard, Wired Mixed Backlit Anti-Ghosting Gaming Keyboards, Wired Mixed Backlit Gaming Keyboard$99.99$79.99 -
Sale!
Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
$119.99$59.99 Add to cartWireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset
Categories: Electronics, Gaming, Gaming Headsets Tags: 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, ANC, Audio, Bluetooth, Cancellation, Ear, Earphone, gaming headset, Headphones, Headset, Hi-Res Over the Ear Headphones Headset, HiRes, Noise, Wireless, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Headphones, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headset, Wireless Bluetooth 5.3 ANC Noise Cancellation Hi-Res Over the Ear Headphones Headsets$119.99$59.99 -
Sale!
Wired Sports Gaming Headset Earbuds with Microphone
$19.99$9.99 Select optionsWired Sports Gaming Headset Earbuds with Microphone
SKU: N/A Categories: Gaming, Gaming Headsets Tags: Accessories, Earbud, Earphone, Earphones, Gaming, gaming headset with microphone, Headphones, Headset, IOS, Microphone, Sports, Wired, Wired Sports Gaming Headset Earbuds, Wired Sports Gaming Headset Earbuds with Microphone, Wired Sports Headset Earbuds$19.99$9.99 -
Sale!
150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
$49.99$29.99 Add to cart150W Universal Multi USB Fast Charger 16 Port MAX Charging Station
Categories: Charging Stations, Electronics Tags: 150W, 150W Charging Station, 150W Universal Multi USB Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Station, 150W Universal Multi USB Fast Charger 16 Port MAX Charging Stations, 150W Universal Multi USB MAX Charging Station, 16 Port MAX Charging Station, 3.5A, Charger, Charging, Fast, laptop charging stations, Max, Multi, Port, Stand, Station, Universal, USB$49.99$29.99
Really clear explanation, thank you.
Great
great! That was really helpful. The debugger seems very daunting at first glance, but you made it very simple to understand. Thank you!
Great tutorial. Very clear and easy to follow.
Thanks. Very helpful!
Speaker: "Mystery"
Autocaptions: "Mr.E"
XD
Thank you! Excellent tutorial
Thanks, excelent tutorial… just what I needed
when will resume take us to the starting point.As you said Resume will execute the remaining code after breakpoint. And at 11.14 you use resume for going to starting point.Please can you explain?