Debugging in Java

by | Dec 18, 2020 | Java

Home » Java » Debugging in Java

Introduction

Debugging in Java is the process of interactively running a program such that one can see how the actors (variables and data structures) are changing their values as a line of code is being compiled. This is a common method to find out errors in your code. You can set an ending marker also known as a breakpoint in your code. The code will only be compiled until that point and the variables will be updated until the end of the breakpoint. The process of debugging is supported in Eclipse.

Setting a Breakpoint

Open the Java editor and navigate to the line where you want to set a breakpoint. Right-click on the left margin of the editor at that line and click on ‘Toggle Breakpoint’.

Starting the Debugger

After you have set a breakpoint in your code, it is now turn to switch on the debugger. Select the class with the main method in it. Go to the Menu Bar at the top of your screen and select Debug As > Java Application. If you have not selected a breakpoint in your code, the compiler will run in the Java perspective. However, if it finds an error in the code, you will receive a prompt asking you to switch to the Debugger perspective. If you click ‘Yes’ the debugger will open immediately and you will be automatically switched to the Debugger perspective.

BreakPoints View

The Breakpoints View shows you all the selected breakpoints and watchpoints. You can also edit or remove them from this space itself. If you want to remove a certain breakpoint, simply remove it from the list. Use the ‘Skip All Breakpoints’ option if you want Eclipse to run the code without the Debugger perspective.

Variables View

After you run a program in the Debugger mode, a variable table will open. This will help you investigate the error in your code and assess how each variable was modified with every line. You have the flexibility to change variable assignments in the Debugger.

Author

1 Comment

  1. Lina

    int mystery = (int)(Math.random()* playGame + 1;

    Reply

Submit a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Author