What is Java Debugging?
Bugs are errors in your programming logic. Debugging is the process of going through our code and finding and eliminating these bugs. Java Debugging is the process of debugging Java programs.
Java IDEs such as Eclipse, Netbeans, IntelliJ, etc. help speed up the process of finding bugs in our Java programs (i.e. they help speed up debugging).
In this article, we will look at how to debug a Java program using Eclipse, which is one of the most popular Java IDEs.
Step 1 – Setting Breakpoints
Breakpoints are flags that you can attach to specific lines in your code. Breakpoints communicate to the Java runtime that you are interested in pausing the program execution once execution reaches that point.
In Eclipse, you can easily add breakpoints by double clicking on the left area besides the line numbers, as shown in the diagram below –
Step 2 – Starting the Debug Session
Once the breakpoints are set, the next step is to start a debug session. To debug our application, right-click on the source file and highlight the option
Debug As. Then click on the option
Java Application. This is illustrated in the screenshot below –
Confirm Perspective Switch popup window will follow. You can click on the
Switch button. This is illustrated in the screenshot below –
Step 3 – Navigating the Java Debugging Session
Once you start the debug session, the JRE will continue the execution of our program until it stumbles upon the first breakpoint that we set earlier in the article. Once it does, it will pause execution that will allow us to inspect the state of the JRE, including values of different variables, the number of threads running, information about the thread that was paused due to hitting the breakpoint, etc.
At this point, we can use the IDE provided features such as Resume, Step into, and Step over to control further execution of our program and help finding the bugs that we are after.
In this article, we looked at what is debugging and how to perform Java debugging using the Eclipse IDE