Using Debug Perspective – setting breakpoints

Exclusive offer: get 50% off this eBook here
Instant Eclipse Application Testing How-to  [Instant]

Instant Eclipse Application Testing How-to [Instant] — Save 50%

An easy-to-use guide on how to test Java applications of any scope using Eclipse IDE with this book and ebook

$14.99    $7.50
by Anatoly Spektor | May 2013 | Java Open Source

The article, Using Debug Perspective, will guide you through the ways of setting up breakpoints and navigate through the code using various breakpoint manipulation options. This article by Anatoly Spektor, author of Instant Eclipse Application Testing How-to, will guide you to learn what breakpoints are and how to use them. After reading this article, you will be able to effectively debug Java applications of any scope. Fortunately, any prior knowledge of Eclipse is not required; thus it is suitable for developers with any level of experience in Eclipse application development and testing.

(For more resources related to this topic, see here.)

In this article we will learn why breakpoints are important, how to set them up, and how to navigate through the code using the Step Into, Step Over, and Step Return breakpoint manipulation options.

Let's practice setting up a breakpoint on our sample program.

How to do it...

I am sure you are aware that in Java, the first method that is executed is the main() method. Our Employee class has its own main() method, so let's set up a breakpoint in it.

If you are on Windows and If your project has more than one class, and the main() method is located in a different class from the one you are about to debug, go to the icon and select Debug | Debug Configurations.... Select the class you are about to debug in the left menu and set the Main class option to the class that has the main() method.

To set up a breakpoint

  1. Go to the main() method of the Employee class.

  2. Find andrew.setNumber(123) (on line 110).

  3. Right-click on the line number (if you don't see the line number, click on the left margin of the Employee tab and choose Show Line Numbers as shown in the following screenshot).

  4. Got to line 110, right-click on the margin again, and click on Toggle Breakpoint.

  5. If your breakpoint is successfully set up, you will see the icon right near the line number of the method you wanted to set the breakpoint to. (In our case right near line number 110.)

    Another way to set a breakpoint is to double-click on the gray field near the line number. To unset the breakpoint, just double-click on it again.

  6. Now let's run the debugger by clicking on the icon.

  7. If you are asked to switch to Debug Perspective, click Yes.

After running the debugger you should see that your program has highlighted the line with the breakpoint, as shown in the following screenshot:

This means that the program has stopped executing before line 110 and is waiting for your actions.

Before we proceed with exploring how Step Into/ Step Over works, let's take a look at the Variables view. Right now there are two lines: args that represents arguments passed to the main() method and andrew. Expand andrew and you will see the current state of all the class variables as shown in the following screenshot:

We will keep on watching this view as it will change when we go through our class.

Let's learn how to navigate through the debugger.

We navigate with the help of the Step Into/Step Over buttons. Let's see how it works.

At this point we are standing at line 110 and there are four options that we can choose. We can Step Into, Step Over, Go to Next breakpoint, or Go Back.

Step Into means that we go inside the method and explore its functionality. Let's try it right now. In the top menu there are two buttons: . The first button is responsible for Step Into (F5) and the second one for Step Over (F6).

  1. Click on the Step Into (F5) icon.

    Now you are taken to the setNumber() method. If you take a look at the Variables view, there are two lines now: this, which relates to the class variables, and _number, which is a variable of the local method.

    Your position is at the if statement evaluating if the number is between certain limits (at line 46).

  2. Click on the Step Into (F5) icon again.

    As there is no method to Step Into, the pointer moves one line down; now it's on number = _number.

    This line assigns local variables to the class variable.

  3. Click on the Step Into (F5) icon again.

    Right now the pointer should be at line 49.

    Now let's look at the Variables view again. If you expand this, you will see that the number is highlighted in yellow. This is because the variables have changed their values. See the following screenshot:

     

    This highlighting is very useful, as it allows you to see what variables have changed their values right after it has happened.

    Before we use Step Into one last time in this exercise, let's also take a look at the Debug view. Sometimes, when the project is big, it is very hard to determine what is the hierarchical structure of the classes and methods, which you are in at the moment. In our case the Debug view shows that we are in the setNumber(int) method on line 49, and if we look below this line, we see that we came here from the main() method on line 110. See the following screenshot:

    If at any time you want to return back to the calling method (in our case it is the main() method) use Step Return (F7) .

  4. Click on the Step Into (F5) icon.

    As we have reached the end of the setNumber() method, Step Into makes it return back to the main() method, and the pointer points to the next line (line 111 in our case). Also, the Debug view no longer shows setNumber() in its hierarchy.

Now, let's consider the situation when you don't want to step into the method but you want to go directly to the next method. In this situation we use Step Over (F6). Note that when you use Step Over, the method is still called but the debugger does not walk you through it.

Let's step over the setAge() method.

Clicking on Step Over (F6), you might see that you were not prompted inside the setAge() method, but directly to line 112. Take a look at the Variables view. You will see that age is set to 28 (that is what the setAge() method does), despite you not walking through this method.

The last two controls that I want to mention in this recipe are Resume (F8) , which allows you to continue running your application until it next faces the breakpoint or the end of the program, and Terminate (Ctrl + F2), which stops the debugging process.

Summary

This article helped you to learn what a breakpoint is. You also learned how to set breakpoints and how to navigate through the debugger using Step Into, Step Over, and Step Return breakpoint manipulation options.

Resources for Article :


Further resources on this subject:


Instant Eclipse Application Testing How-to  [Instant] An easy-to-use guide on how to test Java applications of any scope using Eclipse IDE with this book and ebook
Published: April 2013
eBook Price: $14.99
See more
Select your format and quantity:

About the Author :


Anatoly Spektor

Anatoly Spektor is a blogger, web developer, and a software engineer with more than 4 years’ experience in software and web development fields. Currently, Anatoly works at Red Hat, migrating Eclipse’s Standard Widget Toolkit to a newer graphic library. His main work is directly related to the topics discussed in this book, as he is testing and debugging Eclipse platform issues on an everyday basis. Prior to working at Red Hat, Anatoly has successfully developed a polling module for the open source web conferencing tool Big Blue Button, which gained great support from the open-source community. Besides his main work, Anatoly is a founder of one of the top100 programming blogs – http://myprogrammingblog.com, where he posts programming tutorials. He spends his free time practising Muay Thai, playing tennis, traveling, and spending time with his family and friends.

Books From Packt


 Android 3.0 Application Development Cookbook
Android 3.0 Application Development Cookbook

Eclipse Plugin Development by Example: Beginner's Guide
Eclipse Plugin Development by Example: Beginner's Guide

Instant Eclipse 4 RCP Development How-to [Instant]
Instant Eclipse 4 RCP Development How-to [Instant]

Getting Started with Eclipse Juno
Getting Started with Eclipse Juno

 JBoss Tools 3 Developers Guide
JBoss Tools 3 Developers Guide

Java EE Development with Eclipse
Java EE Development with Eclipse

 Liferay Portal Systems Development
Liferay Portal Systems Development

Selenium 2 Testing Tools: Beginner’s Guide
Selenium 2 Testing Tools: Beginner’s Guide


Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software