Java Debugging with Netbeans
By nitkal on Feb 26, 2008
Debugging can be quite a wearing experience, especially if it has to be done aidlessly. Nevertheless it is a quintessential technique for Software "bug-fixers". Netbeans offers some good features which could help in debugging server, applet or command-line processes. Breakpoints, watches, call stack navigation and such additional features help in seeing the code under the hood. This post is a primer on Debugging with Netbeans
The basic idea of debugging is that there are two processes - one a Serversocket that listens on a specific port and another that attaches itself to that port during the debugging process.Netbeans offers both kinds of sockets - (i) A "SocketAttach" that attaches to the port at which the java process/server process listens to and a (ii) "SocketListen" that listens at the specific port to which another process (say an applet) would attach to.
Steps for Server Process Debugging
This is probably the most widely used form of debugging.
1. Start the server in the debugging mode. (For Glassfish, this is done by setting the debug-enabled flag to true in <DOMAIN-DIR>/config/domains.xml and restarting the server. Note the port no at which the server is listening)
2. In the Netbeans IDE, open the code base which is in sync with the deployed server code.
3. Goto Run-> Attach Debugger -> Choose ServerAttach mode. The NB IDE attaches to the socket opened by the server in this case.
4. Provide details of the server host and port
5. Set breakpoints in the code and launch the application. The thread stops at the breakpoints, and the code could be stepped over or stepped into to debug further.
Steps for Java Command Line Debugging
1. To debug a java command line process, run the command in the debug mode:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=<portNo>,server=y,suspend=y ABCProg
2. Open the code base in Netbeans and Attach the debugger in the SocketAttach mode, provide the host and port information
Steps for Applet Debugging
1. For Applet debugging, go to the Java ControlPanel (In Unix, it is <JAVA_HOME/bin/ControlPanel, in Windows, ControlPanel -> Java). Navigate to the Java tab - View Settings - Edit the Java Runtime settings. Furnish this information:
This makes the browser JRE open a socket connection while launching the applet.
2. Open the NB code base and attach the debugger in the SocketListen mode by furnishing the host and port information.