Suppose you wrote your program and made it bulletproof by catching and properly handling all exceptions. Then you run it, and it does not work right. Now what? (If you never have this problem, you can skip the remainder of this section.)
Of course, it is best if you have a convenient and powerful debugger. Debuggers are available as a part of professional development environments such as Eclipse and NetBeans.
We discuss the debugger later in this chapter. In this section, we offer you a number of tips that may be worth trying before you launch the debugger.
If x is a number, it is converted to its string equivalent. If x is an object, then Java calls its toString method. To get the state of the implicit parameter object, print the state of the this object.
Most of the classes in the Java library are very conscientious about overriding the toString method to give you useful information about the class. This is a real boon for debugging. You should make the same effort in your classes.
Make a few objects, call all methods, and check that each of them does the right thing. You can leave all these main methods in place and launch the Java virtual machine separately on each of the files to run the tests. When you run an applet, none of these main methods are ever called. When you run an application, the Java virtual machine calls only the main method of the startup class.
Whenever the setBackground method is called, a log message is generated. To find out who called the method, generate a stack trace.
You don’t even need to catch an exception to generate a stack trace. Simply insert the statement
anywhere into your code to get a stack trace.
This works in bash and the Windows shell.
This can occasionally be helpful to diagnose class path problems.
We have found that for the flash option to work, you must disable “double buffering,” the strategy used by Swing to reduce flicker when updating a window. The magic incantation for turning on the flash option is Repaint Manager .current Manager get Root Pane()) .set Double Buffering Enabled (false);
Simply place these lines at the end of your frame constructor. When the program runs, you will see the content pane filled in slow motion. Or, for more localized debugging, just call setDebugGraphicsOptions for a single component. Control freaks can set the duration, count, and color of the flashes—see the on-line documentation of the Debug Graphics class for details.
The following options are available:
The jconsole program
In UNIX/Linux, run the ps utility; in Windows, use the task manager. Then launch the jconsole program:
NOTE: Prior to Java SE 6, you need to launch your program with the -Dcom.sun.management.jmxremote option:
You can use the jmap utility to get a heap dump that shows you every object on the heap. Use these commands:
Then, point your browser to localhost:7000. You will get a web application that letsyou drill down into the contents of the heap at the time of the dump.
If you launch the Java virtual machine with the -Xprof flag, it runs a rudimentary profiler that keeps track of the methods in your code that were executed most often. Theprofiling information is sent to System.out. The output also tells you which methodswere compiled by the just-in-time compiler.
Using a Console Window
When you debug an applet, you can see error messages in a window: In the configuration panel of the Java Plug-in, check the Show Java Console box. The Java Console window has a set of scrollbars, so you can retrieve messages that have scrolled off the window. Windows users will find this a definite advantage over the DOS shell window in which the System.out and System.err output normally appears.
We give you a similar window class so you can enjoy the same benefit of seeing your debugging messages in a window when debugging a program. Figure below shows our ConsoleWindow class in action.The class is easy to use. Simply call
Then print to System.out or System.err in the normal way.
The console window
Listing below lists the code for the ConsoleWindow class. As you can see, the class is quite simple. Messages are displayed in a JTextArea inside a JScrollPane. We call the System.setOut and System.setErr methods to set the output and error streams to a special stream that adds all messages to the text area.
Tracing AWT Events
When you write a fancy user interface in Java, you need to know what events AWT sends to what components. Unfortunately, the AWT documentation is somewhat sketchy in this regard. For example, suppose you want to show hints in the status line when the user moves the mouse over different parts of the screen. The AWT generates mouse and focus events that you may be able to trap.
We give you a useful EventTrace class to spy on these events. It prints out all event handling methods and their parameters. See Figure below for a display of the traced events.
The EventTracer class at work
To spy on messages, add the component whose events you want to trace to an event tracer:
That prints a textual description of all events, like this:
You may want to capture this output in a file or a console window, as explained in the preceding sections.
Listing below is the EventTracer class. The idea behind the class is easy even if the implementation is a bit mysterious. Here are the steps that are carried out behind the scenes:
This program is a good example of the power of the reflection mechanism. We don’t have to hardwire the fact that the JButton class has a method addActionListener whereas a JSlider has a method addChangeListener. The reflection mechanism discovers these facts for us.
Letting the AWT Robot Do the Work
Java SE 1.3 added a Robot class that you can use to send keystrokes and mouse clicks to any AWT program. This class is intended for automatic testing of user interfaces. To get a robot, you need to first get a GraphicsDevice object. You get the default screen device through the sequence of calls:
The idea is that you simulate key and mouse input and afterwards take a screen snapshot to see whether the application did what it was supposed to. You capture the screen with the createScreenCapture method:
Finally, you usually want to add a small delay between robot instructions so that the application can catch up. Use the delay method and give it the number of milliseconds to delay. For example:
The program ends by taking a screen capture and displaying it in another frame As you can see from this example, the Robot class is not by itself suitable for convenient user interface testing. Instead, it is a basic building block that can be a foundational part of a testing tool. A professional testing tool can capture, store, and replay user interaction scenarios and find out the screen locations of the components so that mouse clicks aren’t guesswork. Hopefully, as Java applications are becoming more popular, we will see more sophisticated testing tools.
Capturing the screen with the AWT robot
returns the local graphics environment.
returns the default screen device. Note that computers with multiple monitors have one graphics device per screen use the getScreenDevices method to obtain an array of all screen devices.
constructs a robot that can interact with the given device.
simulates a key press or release.
simulates a mouse move.
simulates a mouse button press or release.
Parameters: key The key code. See the KeyStroke class for more information on key codes
Parameters: x, y The mouse position in absolute pixel coordinates Parameters: eventMask The event mask describing the mouse buttons. See the InputEvent class for more information on event masks
delays the robot for the given number of milliseconds.
captures a portion of the screen.
Core Java Related Interview Questions
|J2EE Interview Questions||Core Java Interview Questions|
|JDBC Interview Questions||JSP Interview Questions|
|Android Interview Questions||JavaServer Faces (JSF) Interview Questions|
|Java collections framework Interview Questions||Java 8 Interview Questions|
|Java Collections Interview Questions||Java Exception Handling Interview Questions|
|Java Concurrency Interview Questions||Java Serialization Interview Questions|
|Java Programmer Interview Questions||Java Inheritance Interview Questions|
|Java IO Interview Questions||Object Oriented Programming in PHP Interview Questions|
Core Java Tutorial
An Introduction To Java
The Java Programming Environment
Fundamental Programming Structures In Java
Objects And Classes
Interfaces And Inner Classes
User Interface Components With Swing
Deploying Applications And Applets
Exceptions, Logging, Assertions, And Debugging
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.