As promised, here's my video Java tutorial on how to debug in Java with your Spring STS IDE. This video will give you a hands on explanation on how to debug your code, it will also help you to understand how code flow in Java. It's very useful information so be sure to take it all in!
Transcript of How to Debug in Java Video
Alright, so welcome back to the how to program with Java blog. Today's Java tutorial will be on debugging as promised. Debugging is a really helpful way to figure out if you have any bugs in your program, figure out where they're coming from and give you better idea of how you can fix them. I use it everyday in my job. It's something that every single programmer needs to know how to do, and really, know how to do well. I'm going to teach you how it is that I go about using the Debugger in Spring STS in my day job. That way you can hit the ground running and figure out your own bugs if you happen to create them in your programs.
So here I have a little test program that you might have seen in my last post about exceptions. Slightly modified but you'll get the basic gist of what's going on here. So our runnable class has our main method where we instantiate a new
FileIO Java class that we've created here. It will be instantiated with the constructor that uses a string as an input. So if you go to
FileIO, we'll see that we have a public
FileIO which is the constructor and it takes a
String as a parameter. So we know this is what's going to be called. So in order to debug, there's two things you need to do:
1. Set up an actual debug breakpoint. This can be done by double clicking the left border of your code, or by right clicking the left border (next to the line numbers) and choosing “Toggle on/off”
I prefer double click but it's up to you. Also I put another one in my
FileIO on Line 16. Why do I choose Line 16? Well, you try to double click on Line 13 and it won't let you because you can't put a breakpoint on a line where you are just declaring a variable. So I just put my breakpoint here on this line in the try-block. So that's the first thing you need to know.
2. How to start the program in Debug mode. So you're all familiar with the main method.
Right click on “Main” method -> Choose Run As -> Java application.
But this time instead of using the Run as Java application, we're going to choose Debug as Java application.
Right Click on Main method -> Choose Debug As -> Java application.
It switches to a different perspective. This is called the Debug perspective which you'll see over here. Debug perspective. Really this is just the different view of your code that allows you to better debug. So it's just an easier way to go about debugging. It shows you your variables (on the top right) and any threads that your program maybe running in (or paused in).
So as you can see, we ran our program in Debug mode and we have now a highlighted green line which indicates that the Java code is actually paused and waiting to execute this line of code. So okay, that's cool. How do we actually tell it to execute that line of code? Well at the top of the screen, ares controls that allow us to control the flow of our debug. So here we have the Resume button. So if I click this. It will essentially run the code until either it hits another breakpoint or until the execution completes. The Terminate button, that's pretty self-explanatory. It will stop the Debug mode and it will actually terminate the program right where you are. So it won't actually execute anymore code and it will just stop, exit. Nothing more will happen.
Now here we have “Step Into”. Now this one, I don't use it too frequently because sometimes you'll get into the nitty-gritty code of Java. You'll get into the actual Java class files. I can probably show you what that's like but I don't want to do it just yet. I use it sparingly.
Typically I use “Step Over” which means execute this line of code and just move on to the next line. So if I do that now. If I click “Step Over”, what it's going to do is execute this entire line of code which will in turn instantiate this class. Now, upon instantiation of this class, the code will have to go in to the constructor of the file IO class and execute the code inside the constructor. Well, that's exactly what we looked at over here in the
FileIO. This public
FileIO is the constructor and this code here is everything that's inside of that constructor. Since we put a breakpoint right here at Line 16, the code should stop at Line 16. So let's see if that's what actually happens.
Let's go back to our line here. I’m going to click on “Step Over” and there you go. So now the code has executed the instantiation of our file IO constructor. It's declared our
BufferedReader and it executed the first part of the try-block. And now we are sitting and waiting to instantiate the
BufferedReader. And then it instantiate a
FileReader with the File Name that we are passing in.
In Debug mode, a real bonus here is that we can hover over our variables and it will actually show us what the value of that variable is. Let me just bring that up, there you go.
So we set the variable based on the parameter that we passed in to this constructor which we saw back in My Program. So I actually hard coded it as C:aFile.txt. So I'm going to go ahead and “Step Over” this line. Something interesting will happen. And let’s see what happens. And you would have expected to go to this next line but instead it jumped into our catch block.
Now why did it do that? Well, that's because right now in my current configuration, I don't have aFile.txt stored in the C drive at all. This file doesn't exist. So what happens? Well, it's going to throw an exception. So Java will throw an exception and since we have this line wrapped inside of our try-block. It will then go into the catch block and it will catch the
FileNotFoundException. So we have a
FileNotFoundException that we've actually assigned to a variable name of
And again, like I said, in debug mode you can hover over your variables and it will show you everything there is to know about that particular variable. So we see that as a
FileNotFoundException. We see a detailed message saying, C:\aFile.txt (The system cannot find the file specified), we have a cause and all that stuff. It's helpful to know exactly where the problem came from and this line here
e.printStackTrace() will just output all the important information about the error into our console.
Now, I'll just say “Resume” and let it execute the rest of the code so now the code flows out into completion. You see now that the thread is terminated and we're done.
So this is a very simple example of How to Debug but it's such a powerful tool and I can't overstate how important it is to know how to use the Debugger. Now real quick before we finish here. I just want to show you what the “Step Into” does. I'm going to go back and do the exact same thing as I did before.
Right Click on “Main” method -> Go to Debug as -> Java application.
So we have our breakpoint here set for first line as we had before and instead I'm going to go into the “Step Into”.
You'll see what happens when I click on this. We've now gone into a
ClassLoader.class file which is part of the Java lang package, not very helpful to us. This code, I guess is written by the people who made Java and the possibility of their code breaking this very slim. So it gets really complicated. And if you keep stepping in into this class. I mean it just comes to a point where I don't even have this Java docs loaded so I don't have the sun.misc package on my computer that it can attach to so this is what I get (useless information).
I just want to show you how that is not always the greatest thing unless you know exactly where you’re going to be stepping into and if you know exactly where it’s going to be then it becomes helpful. Like I said, generally not helpful when it's going to be the core Java code.
So let me just let this “Resume” and now once we let it run through, it will have instantiated our file IO class. And now we're back to familiar territory which we've already seen.
So I'll let that run through and we get the same result. So if I were to add this file to the right location, hopefully we'll see something different in the code.
(I rename the text file so that it will be found by my Java code)
Right Click on “main” method -> Choose Debug As -> Java Application -> Step Over or Resume
It doesn’t really matter because I have that second breakpoint. If I resume I get here so now the file should exist there.
So let me “Step Over” this one and there you go. So now if we hover over our
BufferedReader, we have some stuff. And we're able to keep stepping over the code. Now we get into our
while loop and you'll see that it goes to the first
System.out with the line that's been ready for the file which is this is the Line 1 from file.
And we say “step over” again, we'll see some console output so this is a very good way to understand how your code flows. Very useful. I love it. A great way to learn how Java works.
So that brings us to the end of our little tutorial here. Please if you can take some time to click our share buttons on the left hand side of the screen. If you’re watching this on YouTube then follow my link back to my HowToProgramWithJava.com website. If you’re on my website then please by all means share. I really appreciate and I thank you guys very much. And I can't wait to see you next time.