Basics of Debugging Your Code in Eclipse

This Tutorial Covers The Basics of Debugging In Eclipse Using Java as The Language. You Will Also Learn About Applying Breakpoints and Running The Code With Breakpoints:

We explored some of the Common Eclipse Features in our previous tutorial in this Free Eclipse Training Series.

When you start coding, you can never write perfect or even a correct code in the first attempt. Getting your script running perfectly sometimes requires debugging the code to get the output or the output that you are looking for.

I have seen new developers using print statements to understand what is going on in their code. Though that may work for smaller and lesser complex codes, it is not the best way to understand where your code is getting stuck.

How To Debug Your Code In Eclipse

This tutorial will help you in understanding how the basics of debugging work on Eclipse using Java as the language. This section of the Java Eclipse tutorial will give you a good start to debug code like a professional.

What is Debugging?

Debugging is a technique that is used to see your code execute line by line. That means you are able to see the execution of each line of your code and stop at any line of the code and analyze the code, the variables, and the values these variables carry at the time you have halted the execution.

Debugging is used extensively to find runtime errors in your code. Compiler errors are highlighted in the code itself in the Eclipse IDE and with compile-time errors, you cannot proceed to run your program.

Compiler errors highlighted in the code


package myfirstpackage;

public class MyFirstClass {

public static void main(String[] args) {
// TODO Auto-generated method stub

System.out.println("This is my first code")

However, if you have runtime exceptions, then they may not be highlighted in the code, instead, when you run the code, your program will fail due to this exception.

Runtime exceptions may not be highlighted


package myfirstpackage;

public class MyFirstClass {

public static void main(String[] args) {
// TODO Auto-generated method stub
 System.out.println("This is my first code");
String arr[] = new String[]{"a", "b", "c", "d"};

for(int i=0;i<5;i++)

System.out.println("This is my first code2");



Debugging helps for such exceptions. Debugging will let you see how the code enters the “for loop” for every iteration and shows you why it is throwing an error after printing a few options.

In the example we are using, you may be able to understand the issue just by viewing the exception, as the array size is less than the number of times the for loop is running this exception is being thrown. However as this tutorial is more about the debugger, we have chosen this simple code. The same logic can be applied to complex codes as well.

How to Launch the Debugger?

There are multiple ways to launch the debugger on your code. You can launch the debugger through the perspectives in Eclipse.

How to launch the debugger

Debug is the first view available in the list of perspectives. Let’s see what happens when you launch this perspective. Click on the first option in the “Open Perspective” and click on open.

The debug perspective will show the following window:

Debug perspective

  • Debug window: Right next to the project explorer the debug explorer is opened, in which the class being debugged is displayed.
  • Class: This is the class that you want to debug.
  • Variables: This section is where you can view the variables and how their state is changing during execution. By right-clicking on the variables displayed here, you can do multiple operations on them like change them or view their data type, etc.
  • Breakpoint: In this section, you can view and change breakpoints (explained further). From here you can perform advanced operations on breakpoints such as defining conditions on them.
  • Console: This is where you can see the execution happening.

Now as we have the debugger, let’s debug. If you run your code directly at this point, it will run completely and halt when an exception is thrown. However, you want it to halt when it enters the array and from there you want to run every line of the code manually.

The Breakpoint

To take manual control of your program at the time of execution you need something called a breakpoint. A breakpoint is a way to tell the debugger from where you want to take control of your code. Applying a breakpoint is very simple.

On the line where you want the breakpoint, double-click on the left margin, and a blue circle is created next to the line as shown below:

The Breakpoint

If you right-click on this blue circle, you will get an option to toggle (add a breakpoint) or disable the breakpoint. You can also place a breakpoint by placing your cursor on the required code and press the keys “SHIFT + CTRL + B”.

Here we have placed the breakpoint at the start of the for a loop because if you see the exception that we got it is on the line inside this loop, thus you need to place this breakpoint at the code block where you are getting the error.

Running the Code in Debug

Now, you have your program in debug mode and have placed the required breakpoints where we can run the code in debug mode. To run the code in debug mode you can do the following:

  • Click on the icon shown below.

Running the code in debug

  • Right-click on the class name from the project explorer and click on Debug As -> Java Application.

select debug as - java application

Once you run the application in debug mode, the following window will show up.

breakpoint applied is highlighted

You can see, the line on which the breakpoint is applied is highlighted and the execution of code has stopped at that point. You can verify this because the print statement before that line is executed on the console, however, the one inside the loop is not printed.

You can also see the variables until this point is displayed in the variables section. If you expand the variable “arr”, then you can see all the values in it.

expand the variable “arr”

In this manner, we can see the value for any variable in the debug mode.

Now we need to manually enter the loop and we have certain key controls to do the same as shown below:

#1) Step into or F5: This option is also available as the icon shown below.

Step into or F5

Using this, you can execute the line of code you are at and move to the next line.

By clicking this on our code the following happens:

Execute the line of code you are at

If you hover on the variable “i” on this line of code, then you can see its value at two places.

Hover on “i” to see its value in two places

As we need to navigate through the loop till the end, you need to hit F5 again. As soon as you do that, it executes line 13 and moves back to line 11 and hence you can see the first value of array printed in the console.

Array printed in the console

As you keep hitting F5, you will see the value of the variable “i” change and the array values printed on console till the time the exception has occurred. At the time of exception as the code will not get any value from the array, it will show you a source not found kind of page.

This is specific to this error and for different errors, you may get different sort of pages, however, you will find on which line the code is breaking.

#2) Step over of F6: This option is also available as the icon shown below.

Step over of F6

Using this you can skip the debugger and execute the code. In this case, the code will execute normally till you keep hitting F6 and in the end, you will get the exception as you do while normally executing.

Skip the debugger and execute the code

#3) Step out or Step return or F7:  This can be used when your code is in a method and is being called from another place. This key will finish the execution of the method being debugged and return to the code from where this method is being called. This can also be done by clicking on the below icon.

Step out or F7

#4) Resume or F8: This option will tell the debugger to continue executing the program until the next breakpoint is reached. The icon for this is given below.

17. Resume


In this tutorial we have learned about the following:

  1. Debugger perspective
  2. Applying breakpoints to your code.
  3. Running your code with breakpoints manually.

These topics cover the basics of debugging, however, they are a good start. There are more advanced ways to debug as well where you can define properties on your breakpoints and create watchpoints on your fields. However, once you are comfortable with the basics, you can dive into the more complex debugging.

PREV Tutorial | NEXT Tutorial