Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
Java Debugger
Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb. It works like a command-line debugger for Java classes.
 
 

Java Debugger

                         

Java debugger helps in finding and the fixing of bugs in Java language programs. The Java debugger is denoted as jdb. It works like a command-line debugger for Java classes. 

jdb session

The way to start the jdb session is to have jdb launch a new JVM (Java virtual machine) with the main class. Then the application of the main class is debugged by substituting the command jdb in command-line. For instance, if the main class of the application is TempClass, the following command is used to debug it:

% jdb TempClass  

There is another way to use jdb i.e.to attach jdb to Java VM which is already running. 

There are few options which are used to debug a VM with jdb. These are:

option  purpose
-Xdebug  Enables debugging in the VM
-Xrunjdwp:transport=dt_socket,server=y,suspend=n  Loads in-process debugging libraries and specifies the kind of connection to be made

The following command will run the TempClass application to which the jdb will connect afterwords.

% java -Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n TempClass

Now the jdb will be attached to the VM in this way:  % jdb -attach 8000 

You can go through the basic jdb commands that the Java debugger supports.

cont
Ihis command Continues the execution of the debugged application after a breakpoint, exception, or step. 

run
Use run command to start the execution after starting jdb, and setting any necessary breakpoints,  use run command to start the execution . When jdb launches the debugged application then only this command is available. 

print
This command is used to display Java objects and primitive values. The actual value is printed for the variables or fields of primitive types. For objects, a short description is printed. 

Few examples of print command are:
 
print TempClass.myStaticField
 print myObj.myInstanceField
 print myObj.myMethod() 

dump
 This command is similar to print command. For objects, it is used to print the current value of each field defined in the object including Static and instance fields.
The dump command supports the same set of expressions as the print command. 

Exceptions
If any kind of exception occurs in the program, the VM will print an exception trace and exits. It will only print this exception when there isn't any catch statement in the throwing thread's call stack.
There is another command to be used to stop the debugged applications at other thrown exceptions. The command is catch command. For instance, 
"catch java.io.FileNotFoundException" or "catch mypackage.BigTroubleException. 
Also the ignore command negates the effect of a previous catch command.

help, or ?
The command which helps in displaying the list of recognized commands is the help or ? command.

threads
This command list the threads that are currently running. The name and current status are printed for each thread. The index is also printed that can be used for other commands, for example:
4. (java.lang.Thread)0x1 main running
This example shows that the thread index is 4, the thread is an instance of java.lang.Thread, the thread name is "main", and it is currently running.

thread
This command selects a thread to be the current thread. Some jdb commands are based on the setting of the current thread. The thread index specifies the thread as explained in the threads command above. 

where
The command where is used to dump the stack of the current thread. Whereas the command where all is used to dump the stack of all threads in the current thread group. And the where thread index command is used to dump the stack of the specified thread.

Breakpoints
The way to use Breakpoints is to set it in jdb at line numbers or at the first instruction of a method, for example:
 stop at TempClass:14 (sets a breakpoint at the first instruction for line 14 of the source file containing TempClass)
 stop in TempClass.<init> (<init> identifies the TempClass constructor)

It is essential to specify the argument types whenever we overload any method in order to select  the proper method for a breakpoint. For example,
"TempClass.myMethod(int,java.lang.String)", or "TempClass.myMethod()".
We also use the clear command to remove breakpoints using a syntax as in "clear TempClass:20" and the cont command continues execution. 

Command Line Options

 Use jdb in place of the Java application launcher on the command line, it accepts most of the same options as the java command, which includes -D, -classpath, and -X<option>.
Some more options by jdb:
-sourcepath <dir1:dir2:...>
Uses the given path in searching for source files in the specified path. If this option is not specified, the default path of "." is used. 
-attach <address>
Attaches the debugger to previously running VM using the default connection mechanism. 
-launch
As soon as the jdb start sup,launch option launches the debugged application immediately. We need not to use the run command after using this option. 
-Joption
It passes option to the Java virtual machine, where option is one of the options described on the reference page for the java application launcher. For example,
-J-Xms48m sets the startup memory to 48 megabytes.

Lets tweak an example:

public class Y {
     public static int add(int a, int b)
	{
		return a+b;
	}
	public static int sub(int a, int b)
	{
		return a-b;
	}
	public static void main(String args[]) 
	{
		int a = 10;
		int b = 20;
		int c;		
		c = add(a, b);
        System.out.println(c);
		c = sub(a, b);
		System.out.println(c);
  } 
}

 

After compiling and running the above program, we will initialize the java debugger and we will use the Stop command to out a breakpoint. After that we will use run command to start the jdb. In the similar way we can use other commands as well.

C:\javac>jdb Y
Initializing jdb ...
> stop at Y:6
Deferring breakpoint Y:6.
It will be set after the class is loaded.
> run
run Y
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
>
VM Started: Set deferred breakpoint Y:6

Breakpoint hit: "thread=main", Y.add(), line=6 bci=0
6 return a+b;

main[1] where
[1] Y.add (Y.java:6)
[2] Y.main (Y.java:17)
main[1] methods Y
** methods list **
Y <init>()
Y add(int, int)
Y sub(int, int)
Y main(java.lang.String[])
java.lang.Object <init>()
java.lang.Object registerNatives()
java.lang.Object getClass()
java.lang.Object hashCode()
java.lang.Object equals(java.lang.Object)
java.lang.Object clone()
java.lang.Object toString()
java.lang.Object notify()
java.lang.Object notifyAll()
java.lang.Object wait(long)
java.lang.Object wait(long, int)
java.lang.Object wait()
java.lang.Object finalize()
java.lang.Object <clinit>()


Back to Java Introduction

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.