To access the contents, click the chapter and section titles.
Sams Teach Yourself Visual J++ 6 in 21 Days
(Publisher: Macmillan Computer Publishing)
Author(s): Rick Leinecker
ISBN: 0672313510
Publication Date: 11/01/98
Day 4 Debugging Java Applets and Applications
The Visual J++ debugger might be as important as any other part of Visual J++. Debugging programs effectively is almost as important as writing good codeunless, that is, your code is always perfect. But for the rest of us competent-but-not-perfect programmers, the debugger gets us out of trouble more times than we can count.
In the early days of computers, the guys who wrote assemblers and compilers didnt think debuggers were necessary. They reasoned that if a person wrote the code, that person would know how it should work and wouldnt make any mistakes. That theory was soon shot to pieces. The same guys then wrote debuggers for their assemblers and compilers, much to the relief of software developers.
Todays lesson is divided into two sections. In the first section, youll learn how to use the Visual J++ debugger. Youll learn about the debug windows and how to navigate through your code with the debugger. In the second part of the chapter, youll learn hints and tips for effective debugging. This is no trivial matter because effective debugging requires some of the most difficult reasoning in software development.
In this chapter, youll learn the following:
- How to prepare projects for debugging
- How to start a debug session
- How to step through a project with the debugger
- About the debugger windows
- How to set and use breakpoints
- How to effectively debug programs
If you can master all the techniques listed previously, youll be equipped for the remainder of your Visual J++ journey. If you know how to use the debugger from the start, your development will be much easier and less frustrating because youll know how to track down the errors that baffle others who dont rely on the debugger.
Preparing Projects for Debugging
You can compile projects in one of two ways: with debug code and without debug code. If you compile a project with debug code, the compiler inserts a lot of extra information into the compiled program file (whether its an applet .class file or an application .exe file). This extra information is used by the debugger. Included is symbolic information that tells the debugger all the details about variables and classes. This is very important because during a debug session youre going to want to know about variables and what values are stored in them. Another very important classification of information thats stored relates the compiled bytecode to the source code. The debugger can look at this and know what line of bytecode goes with what line of source code. Knowing this allows the debugger to execute the program while displaying the correct source code.
Before starting a debug session, you must make sure that all modules of a program are compiled with debug code. Start by going to the Build menu. The Build Configuration menu item gives you two choices: Debug and Release. Debug obviously creates a program with embedded debug code. Release creates a program with no embedded debug code. Figure 4.1 shows how your project should be set so that it will compile debug code into the program.
Figure 4.1 Make sure that the Build Configuration is set to Debug before you build your project so that debug code is embedded into the compiled program.
If youre not sure that all the modules in your program have been compiled with debug code, its a good idea to select Build, Rebuild. This step recompiles all modules in your project. After youre sure that all the modules have been recompiled with debug code, you can safely use Build, Build and have only out-of-date modules rebuilt.
Note: Its not wise to upload class files with debug code to the Web, or even deploy them as applications. They contain a lot of extra information that release programs shouldnt have. As a general rule, the debug versions of programs are from two to four times as large as the release versions.
Starting a Debugging Session
You can start a debugging session in several ways. Lets first start by creating an application with which well explore the world of debugging. First, create a Windows Application project named LearnToDebug. Make sure that the Build Configuration has Debug selected. (This is the default, so it should be.) Now build the project by selecting Build, Build. Follow the steps listed here to create and build the program:
- 1. Select File, New Project. The New Project dialog box will appear.
- 2. Make sure that the New tab is selected in the New Project dialog box.
- 3. In the window in the left side of the New Project dialog box that has the Visual J++ Projects folder, open the applications folder.
- 4. In the window in the right side of the New Project dialog box, select Windows Application.
- 5. In the Name field, type LearnToDebug.
- 6. Click the Open button.
- 7. Select Build, Rebuild.
If you select Debug, Start, the program will execute inside of the debugger. The problem is that Start runs the program and stops only when a breakpoint is encountered. Because we just created and built this program, no breakpoints are set. If youve selected Start and the program is running, you can stop it by closing the application or by selecting Debug, End.
You might notice that the Start item in the Debug menu changes to Continue while the program is executing (even if its stopped at a breakpoint).
With the program running, you can also select Debug, Break. The program will pause wherever it currently is. With the program paused, you can resume the program by selecting Debug, Continue.
The Start (Continue), End, and Break commands can be found in the Debug menu. There are also default shortcut keys you can use, as shown in Table 4.1.
Table 4.1 Default Debug Keys
|
Shortcut Key
| Action
|
|
F5
| Start/Continue
|
Ctrl+F5
| Start Without Debugging
|
Ctrl+Break
| Break
|
Shift+F5
| End
|
Ctrl+Shift+F5
| Restart
|
Ctrl+F10
| Run To Cursor
|
F11
| Step Into
|
F10
| Step Over
|
Shift+F11
| Step Out
|
Ctrl+F9
| Enable Breakpoint
|
Ctrl+Shift+F9
| Clear Breakpoints
|
Ctrl+B
| Breakpoint Dialog
|
|
Tip: All the shortcut keys in Visual J++ can be changed. To change them, select Tools, Options, and the Options dialog box will appear. In the Environment category, select Keyboardyoull see the keyboard category and commands lists. To change the shortcut keys for debugging, select Debug in the category list. The available options will appear in the commands list.
When you run an application in debug mode, Visual J++ invokes its debugger. But when you run an applet in debug mode, it first runs Internet Explorer, which in turn interacts with the debugger. Applets that are run in the debugger take anywhere from 15 seconds to three minutes to begin execution. Thats because of the number of things Visual J++ and its components must do to get Internet Explorer and everything else running in debug mode.
|