Simple use of JDB

Source: Internet
Author: User

 


When a newbie starts learning Java, a problem that will be discovered immediately after the first freshness is how to debug it. We all know that Visual C ++ provides a good debugging tool, which is especially convenient to use. In Java, taking JDK as an example, there is no convenient graphical interface, so it brings a lot of difficulties to novice debugging. At first, many people use System. out. println () to observe the output results. If a large program is written, this method is obviously inefficient. The following describes how to use the debugging tool JDB in Java based on your learning experience.
Environment: jdk 1.2.2

First, we write a simple applet, but it contains some basic object-oriented elements.
Class test
{
Int;
Int B;
Test (int aa, int bb)
{
A = aa;
B = bb;
}
Int add ()
{Return a + B ;}
}
Public class hehe
{
Public static void main (String args [])
{
Int a = 2;
Int B = 3;
Int c = a + B;
System. out. println (c );
Test kk = new test (1, 2 );
System. out. println (kk. add ());
}
}

After saving as hehe. java, use javac-g hehe. java for compilation. The parameter g is used to generate various debugging information and cannot be debugged without any need. If you have any problems, please refer to the Helloworld strategy. The above program can be passed and can be run directly using java hehe. The following uses this example to describe how to use JDB.

First, type jdb hehe. If the following information is displayed, the debugging class is not found. You can use the java-classpath. hehe command to solve the problem.
C: javasource> jdb hehe
Initializing jdb...
Hehe not found
>

If a message is displayed, Debugging starts and everything is normal. To debug the Applet, run the appletviewer-debug hehehe.html command.
C: javasource> jdb-classpath. hehe
Initializing jdb...
0xb0: class (hehe)
>

Recall that the debugging in VC should be to set the breakpoint before tracking. The same is true in Java. Use the stop command to set breakpoints. Run the run Command to start debugging and run the program to the breakpoint. the breakpoint is set in the main function.
> Stop at hehe: 18
Breakpoint set at hehe: 18
> Run
Run hehe
Running...
Main [1]
Breakpoint hit: hehe. main (hehe: 18)
Main [1]

In this case, you can use the locals command to view the variables, use the step command to enter the next command, or use a separate stop command to view the breakpoint settings. Note that B is not assigned a value. Main [1] locals
Method arguments:
Local variables:
Args =
A = 2
Main [1] step
Main [1]
Breakpoint hit: hehe. main (hehe: 19)
Main [1]

When you run the System. out. println () function, the following prompt appears:
Main [1] step
Main [1]
Breakpoint hit: java. lang. ClassLoader. loadClass (ClassLoader: 247)

This is because we have tracked the println method. We generally do not need to do this. In this case, we can use next to skip this method and enter the next sentence. Step indicates entering function tracing, and next indicates transferring to the next statement for execution. You can enter the locals and list commands at any time to view the variable value and the currently running code. The Arrow below indicates the place where the current program runs.
Main [1] next
Main [1]
Breakpoint hit: hehe. main (hehe: 20)
Main [1] list
16 {
17 int a = 2;
18 int B = 3;
19 int c = a + B;
20 => System. out. println (c );
21 test kk = new test (1, 2 );
22 System. out. println (kk. add ());
23
24}
Main [1]

The next question is how to view objects. When the program runs the new command, type locals and you can see the main [1] step
Main [1]
Breakpoint hit: test. (test: 5)
Main [1] list
1 class test
2 {
3 int;
4 int B;
5 => test (int aa, int bb)
6 {
7 a = aa;
8 B = bb;
9}
Main [1] locals
Method arguments:
Local variables:
This = test @ 64fd6722
Aa = 1
Bb = 2
Main [1]

The displayed variable value is the variable value in the constructor class test. This object is the currently constructed object. You can run the dump command to view the information.
Main [1] dump this
This = (test) 0x11a {
Int B = 0
Int a = 0
}

You can also run the dump kk and print commands in the main function to view the object. main [1] dump kk
Kk = (test) 0x11a {
Int B = 2
Int a = 1
}
Main [1] print kk
Kk = test @ 64fd6722
Main [1] print kk.
Kk. a = 1
Main [1] print kk. B
Kk. B = 2

Finally, enter the cont command. If no other breakpoint exists, the program runs and exits. Debugging is complete. Main [1] cont
3

> Current thread "main" died. Execution continuing...
>
Hehe exited

The breakpoints in the above operations are all set in the main function. If you want to set them in the called class method, use stop in yourclassname. functionname command, for example:> stop in test. add
Breakpoint set in test. add
> Run
Run hehe
Running...
Main [1] 5
Breakpoint hit: test. add (test: 11)
Main [1] list
7 a = aa;
8 B = bb;
9}
10 int add ()
11 ==>{ return a + B ;}
12}
13 public class hehe
14 {
15 public static void main (String args [])
Main [1]

In this way, we can set and track breakpoints in almost all the places needed in the program to view the variables.
JDB also has many debugging methods. In addition to the most common ones above, the other important ones are clear breakpoints. use sets the source program path and memory displays the current memory usage, gc forcibly recycles memory ,!! Repeat the preceding command to set the current thread, such as quit and exit jdb, as well as remote debugging. Here we will not introduce them one by one.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.