Home > Articles > Programming > Java

This chapter is from the book

Using a Debugger

Debugging with print statements is not one of life's more joyful experiences. You constantly find yourself adding and removing the statements, then recompiling the program. Using a debugger is better. A debugger runs your program in full motion until it reaches a breakpoint, and then you can look at everything that interests you.

The JDB Debugger

The JDK includes JDB, an extremely rudimentary command-line debugger. Its user interface is so minimal that you will not want to use it except as a last resort. It really is more a proof of concept than a usable tool. We nevertheless briefly introduce it because there are situations in which it is better than no debugger at all. Of course, many Java programming environments have far more convenient debuggers. The main principles of all debuggers are the same, and you may want to use the example in this section to learn to use the debugger in your environment instead of JDB.

Examples 11-9 through 11-11 show a deliberately corrupted version of the ButtonTest program from Chapter 8. (We broke up the program and placed each class into a separate file because some debuggers have trouble dealing with multiple classes in the same file.)

When you click on any of the buttons, nothing happens. Look at the source code—button clicks are supposed to set the background color to the color specified by the button name.

Example 11-9. BuggyButtonTest.java

 1. import javax.swing.*;
 2.
 3. public class BuggyButtonTest
 4. {
 5.   public static void main(String[] args)
 6.   {
 7.     BuggyButtonFrame frame = new BuggyButtonFrame();
 8.     frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 9.     frame.setVisible(true);
10.   }
11. }

Example 11-10. BuggyButtonFrame.java

 1. import java.awt.*;
 2. import javax.swing.*;
 3.
 4. public class BuggyButtonFrame extends JFrame
 5. {
 6.    public BuggyButtonFrame()
 7.    {
 8.       setTitle("BuggyButtonTest");
 9.       setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
10.
11.       // add panel to frame
12.
13.       BuggyButtonPanel panel = new BuggyButtonPanel();
14.       add(panel);
15.    }
16.
17.    public static final int DEFAULT_WIDTH = 300;
18.    public static final int DEFAULT_HEIGHT = 200;
19. }

Example 11-11. BuggyButtonPanel.java

 1. import java.awt.*;
 2. import java.awt.event.*;
 3. import javax.swing.*;
 4.
 5. class BuggyButtonPanel extends JPanel
 6. {
 7.    public BuggyButtonPanel()
 8.    {
 9.       ActionListener listener = new ButtonListener();
10.
11.       JButton yellowButton = new JButton("Yellow");
12.       add(yellowButton);
13.       yellowButton.addActionListener(listener);
14.
15.       JButton blueButton = new JButton("Blue");
16.       add(blueButton);
17.       blueButton.addActionListener(listener);
18.
19.       JButton redButton = new JButton("Red");
20.       add(redButton);
21.       redButton.addActionListener(listener);
22.    }
23.
24.    private class ButtonListener implements ActionListener
25.    {
26.       public void actionPerformed(ActionEvent event)
27.       {
28.          String arg = event.getActionCommand();
29.          if (arg.equals("yellow"))
30.             setBackground(Color.yellow);
31.          else if (arg.equals("blue"))
32.             setBackground(Color.blue);
33.          else if (arg.equals("red"))
34.             setBackground(Color.red);
35.       }
36.    }
37. }

In a program this short, you may be able to find the bug just by reading the source code. Let us pretend that scanning the source code for errors is not practical. Here is how you can run the debugger to locate the error.

To use JDB, you must first compile your program with the -g option, for example:

javac -g BuggyButtonTest.java BuggyButtonFrame.java BuggyButtonPanel.java

When you compile with this option, the compiler adds the names of local variables and other debugging information into the class files. Then you launch the debugger:

jdb BuggyButtonTest

Once you launch the debugger, you will see a display that looks something like this:

Initializing jdb...
>

The > prompt indicates the debugger is waiting for a command. Table 11-4 shows all the debugger commands. Items enclosed in [...] are optional, and the suffix (s) means that you can supply one or more arguments separated by spaces.

Table 11-4. Debugging Commands

threads [threadgroup]

Lists threads

thread thread_id

Sets default thread

suspend [thread_id(s)]

Suspends threads (default: all)

resume [thread_id(s)]

Resumes threads (default: all)

where [thread_id] or all

Dumps a thread's stack

wherei [thread_id] or all

Dumps a thread's stack and program counter info

threadgroups

Lists thread groups

threadgroup name

Sets current thread group

print name(s)

Prints object or field

dump name(s)

Prints all object information

locals

Prints all current local variables

classes

Lists currently known classes

methods class

Lists a class's methods

stop in class.method

Sets a breakpoint in a method

stop at class:line

Sets a breakpoint at a line

up [n]

Moves up a thread's stack

down [n]

Moves down a thread's stack

clear class:line

Clears a breakpoint

step

Executes the current line, stepping inside calls

stepi

Executes the current instruction

step up

Executes until the end of the current method

next

Executes the current line, stepping over calls

cont

Continues execution from breakpoint

catch class

Breaks for the specified exception

ignore class

Ignores the specified exception

list [line]

Prints source code

use [path]

Displays or changes the source path

memory

Reports memory usage

gc

Frees unused objects

load class

Loads Java class to be debugged

run [class [args]]

Starts execution of a loaded Java class

!!

Repeats last command

help (or ?)

Lists commands

exit (or quit)

Exits debugger

We cover only the most useful JDB commands in this section. The basic idea, though, is simple: you set one or more breakpoints, then run the program. When the program reaches one of the breakpoints you set, it stops. You can inspect the values of the local variables to see if they are what they are supposed to be.

To set a breakpoint, use


stop in class.method

or the command


stop at class:line

For example, let us set a breakpoint in the actionPerformed method of BuggyButtonTest. To do this, enter

stop in BuggyButtonPanel$ButtonListener.actionPerformed

Now we want to run the program up to the breakpoint, so enter

run

The program will run, but the breakpoint won't be hit until Java starts processing code in the actionPerformed method. For this, click on the Yellow button. The debugger breaks at the start of the actionPerformed method. You'll see:

Breakpoint hit: thread="AWT-EventQueue-0", BuggyButtonPanel$ButtonListener.actionPerformed
ccc.gif(), line=28, bci=0
   28          String arg = event.getActionCommand();
   

The list command lets you find out where you are. The debugger will show you the current line and a few lines above and below. You also see the line numbers. For example:

24      private class ButtonListener implements ActionListener
25      {
26         public void actionPerformed(ActionEvent event)
27         {
28=>          String arg = event.getActionCommand();
29            if (arg.equals("yellow"))
30               setBackground(Color.yellow);
31            else if (arg.equals("blue"))
32               setBackground(Color.blue);
33            else if (arg.equals("red"))

Type locals to see all local variables. For example,

Method arguments:
  event = instance of java.awt.event.ActionEvent(id=698)
Local variables:

For more detail, use


dump variable

For example,

dump event

displays all instance fields of the evt variable.

event = instance of java.awt.event.ActionEvent(id=698) {
   SHIFT_MASK: 1
   CTRL_MASK: 2
   META_MASK: 4
   ALT_MASK: 8
   ACTION_FIRST: 1001
   ACTION_LAST: 1001
   ACTION_PERFORMED: 1001
   actionCommand: "Yellow"
   modifiers: 0
   serialVersionUID: -7671078796273832149
   . . .

There are two basic commands to single-step through a program. The step command steps into every method call. The next command goes to the next line without stepping inside any further method calls. Type next twice and then type list to see where you are.

The program stops in line 31.

27         {
28            String arg = event.getActionCommand();
29            if (arg.equals("yellow"))
30               setBackground(Color.yellow);
31=>          else if (arg.equals("blue"))
32               setBackground(Color.blue);
33            else if (arg.equals("red"))
34               setBackground(Color.red);
35         }

That is not what should have happened. The program was supposed to call setColor(Color.yellow) and then exit the method.

Dump the arg variable.

arg = "Yellow"

Now you can see what happened. The value of arg was "Yellow", with an uppercase Y, but the comparison tested

if (arg.equals("yellow"))

with a lowercase y. Mystery solved.

To quit the debugger, type:

quit

As you can see from this example, the jdb debugger can be used to find an error, but the command-line interface is very inconvenient. Remember to use list and locals whenever you are confused about where you are. But if you have any choice at all, use a better debugger for serious debugging work.

The Eclipse Debugger

Eclipse has a modern and convenient debugger that has many of the amenities that you would expect. In particular, you can set breakpoints, inspect variables, and single-step through a program.

To set a breakpoint, move the cursor to the desired line and select Run -> Toggle Line Breakpoint from the menu. The breakpoint line is highlighted (see Figure 11-8).

11fig08.jpgFigure 11-8 Breakpoints in the Eclipse debugger

To start debugging, select Run -> Debug As -> Java Application from the menu. The program starts running. Set a breakpoint in the first line of the actionPerformed method.

When the debugger stops at a breakpoint, you can see the call stack and the local variables (see Figure 11-9).

11fig09.jpgFigure 11-9 Stopping at a breakpoint

To single-step through the application, use the Run -> Step into (F5) or Run -> Step over (F6) commands. In our example, press the F6 key twice to see how the program skips over the setBackground(Color.yellow) command. Then watch the value of arg to see the reason (see Figure 11-10).

11fig10.jpgFigure 11-10 Inspecting variables

As you can see, the Eclipse debugger is much easier to use than JDB because you have visual feedback to indicate where you are in the program. Setting breakpoints and inspecting variables is also much easier. This is typical of debuggers that are a part of an integrated development environment.

This chapter introduced you to exception handling and gave you some useful hints for testing and debugging. The next chapter covers streams.

InformIT Promotional Mailings & Special Offers

I would like to receive exclusive offers and hear about products from InformIT and its family of brands. I can unsubscribe at any time.

Overview


Pearson Education, Inc., 221 River Street, Hoboken, New Jersey 07030, (Pearson) presents this site to provide information about products and services that can be purchased through this site.

This privacy notice provides an overview of our commitment to privacy and describes how we collect, protect, use and share personal information collected through this site. Please note that other Pearson websites and online products and services have their own separate privacy policies.

Collection and Use of Information


To conduct business and deliver products and services, Pearson collects and uses personal information in several ways in connection with this site, including:

Questions and Inquiries

For inquiries and questions, we collect the inquiry or question, together with name, contact details (email address, phone number and mailing address) and any other additional information voluntarily submitted to us through a Contact Us form or an email. We use this information to address the inquiry and respond to the question.

Online Store

For orders and purchases placed through our online store on this site, we collect order details, name, institution name and address (if applicable), email address, phone number, shipping and billing addresses, credit/debit card information, shipping options and any instructions. We use this information to complete transactions, fulfill orders, communicate with individuals placing orders or visiting the online store, and for related purposes.

Surveys

Pearson may offer opportunities to provide feedback or participate in surveys, including surveys evaluating Pearson products, services or sites. Participation is voluntary. Pearson collects information requested in the survey questions and uses the information to evaluate, support, maintain and improve products, services or sites, develop new products and services, conduct educational research and for other purposes specified in the survey.

Contests and Drawings

Occasionally, we may sponsor a contest or drawing. Participation is optional. Pearson collects name, contact information and other information specified on the entry form for the contest or drawing to conduct the contest or drawing. Pearson may collect additional personal information from the winners of a contest or drawing in order to award the prize and for tax reporting purposes, as required by law.

Newsletters

If you have elected to receive email newsletters or promotional mailings and special offers but want to unsubscribe, simply email information@informit.com.

Service Announcements

On rare occasions it is necessary to send out a strictly service related announcement. For instance, if our service is temporarily suspended for maintenance we might send users an email. Generally, users may not opt-out of these communications, though they can deactivate their account information. However, these communications are not promotional in nature.

Customer Service

We communicate with users on a regular basis to provide requested services and in regard to issues relating to their account we reply via email or phone in accordance with the users' wishes when a user submits their information through our Contact Us form.

Other Collection and Use of Information


Application and System Logs

Pearson automatically collects log data to help ensure the delivery, availability and security of this site. Log data may include technical information about how a user or visitor connected to this site, such as browser type, type of computer/device, operating system, internet service provider and IP address. We use this information for support purposes and to monitor the health of the site, identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents and appropriately scale computing resources.

Web Analytics

Pearson may use third party web trend analytical services, including Google Analytics, to collect visitor information, such as IP addresses, browser types, referring pages, pages visited and time spent on a particular site. While these analytical services collect and report information on an anonymous basis, they may use cookies to gather web trend information. The information gathered may enable Pearson (but not the third party web trend services) to link information with application and system log data. Pearson uses this information for system administration and to identify problems, improve service, detect unauthorized access and fraudulent activity, prevent and respond to security incidents, appropriately scale computing resources and otherwise support and deliver this site and its services.

Cookies and Related Technologies

This site uses cookies and similar technologies to personalize content, measure traffic patterns, control security, track use and access of information on this site, and provide interest-based messages and advertising. Users can manage and block the use of cookies through their browser. Disabling or blocking certain cookies may limit the functionality of this site.

Do Not Track

This site currently does not respond to Do Not Track signals.

Security


Pearson uses appropriate physical, administrative and technical security measures to protect personal information from unauthorized access, use and disclosure.

Children


This site is not directed to children under the age of 13.

Marketing


Pearson may send or direct marketing communications to users, provided that

  • Pearson will not use personal information collected or processed as a K-12 school service provider for the purpose of directed or targeted advertising.
  • Such marketing is consistent with applicable law and Pearson's legal obligations.
  • Pearson will not knowingly direct or send marketing communications to an individual who has expressed a preference not to receive marketing.
  • Where required by applicable law, express or implied consent to marketing exists and has not been withdrawn.

Pearson may provide personal information to a third party service provider on a restricted basis to provide marketing solely on behalf of Pearson or an affiliate or customer for whom Pearson is a service provider. Marketing preferences may be changed at any time.

Correcting/Updating Personal Information


If a user's personally identifiable information changes (such as your postal address or email address), we provide a way to correct or update that user's personal data provided to us. This can be done on the Account page. If a user no longer desires our service and desires to delete his or her account, please contact us at customer-service@informit.com and we will process the deletion of a user's account.

Choice/Opt-out


Users can always make an informed choice as to whether they should proceed with certain services offered by InformIT. If you choose to remove yourself from our mailing list(s) simply visit the following page and uncheck any communication you no longer want to receive: www.informit.com/u.aspx.

Sale of Personal Information


Pearson does not rent or sell personal information in exchange for any payment of money.

While Pearson does not sell personal information, as defined in Nevada law, Nevada residents may email a request for no sale of their personal information to NevadaDesignatedRequest@pearson.com.

Supplemental Privacy Statement for California Residents


California residents should read our Supplemental privacy statement for California residents in conjunction with this Privacy Notice. The Supplemental privacy statement for California residents explains Pearson's commitment to comply with California law and applies to personal information of California residents collected in connection with this site and the Services.

Sharing and Disclosure


Pearson may disclose personal information, as follows:

  • As required by law.
  • With the consent of the individual (or their parent, if the individual is a minor)
  • In response to a subpoena, court order or legal process, to the extent permitted or required by law
  • To protect the security and safety of individuals, data, assets and systems, consistent with applicable law
  • In connection the sale, joint venture or other transfer of some or all of its company or assets, subject to the provisions of this Privacy Notice
  • To investigate or address actual or suspected fraud or other illegal activities
  • To exercise its legal rights, including enforcement of the Terms of Use for this site or another contract
  • To affiliated Pearson companies and other companies and organizations who perform work for Pearson and are obligated to protect the privacy of personal information consistent with this Privacy Notice
  • To a school, organization, company or government agency, where Pearson collects or processes the personal information in a school setting or on behalf of such organization, company or government agency.

Links


This web site contains links to other sites. Please be aware that we are not responsible for the privacy practices of such other sites. We encourage our users to be aware when they leave our site and to read the privacy statements of each and every web site that collects Personal Information. This privacy statement applies solely to information collected by this web site.

Requests and Contact


Please contact us about this Privacy Notice or if you have any requests or questions relating to the privacy of your personal information.

Changes to this Privacy Notice


We may revise this Privacy Notice through an updated posting. We will identify the effective date of the revision in the posting. Often, updates are made to provide greater clarity or to comply with changes in regulatory requirements. If the updates involve material changes to the collection, protection, use or disclosure of Personal Information, Pearson will provide notice of the change through a conspicuous notice on this site or other appropriate way. Continued use of the site after the effective date of a posted revision evidences acceptance. Please contact us if you have questions or concerns about the Privacy Notice or any objection to any revisions.

Last Update: November 17, 2020