by Eric D. Larson
June 2002
Question
How do I use the Java Debugger (JDB) to debug MIDlets?
Answer
Debugging a MIDlet is not quite as easy as debugging a standalone application. Debugging MIDlets resembles debugging servlets: An application server starts the servlet in debug mode, then the debugger connects remotely. Likewise, an emulator starts the MIDlet in debug mode, then a debugger is able to connect remotely.
Using the J2ME Wireless Toolkit, you simply choose Project > Debug from the menu. You are asked for a port number (the default 5000 is typically fine), and the MIDlet is started with debugging information included. The JVM then waits for the debugger to connect to port 5000. To start jdb (or any other debugger for that matter), simply tell it to connect to a remote VM on port 5000. Once connected, the debugger has control of your MIDlet.
This image shows the toolkit side of things:
Image 1: Debugging Mode in the Wireless Toolkit
|
A debugging session might look like this (debugger prompts are in bold):
% jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,port=5000
Initializing jdb ...
>
>
VM Started: "thread=KVM_main", java.lang.Class.runCustomCode(), line=285 bci=0
KVM_main[1] stop in Jargoneer.commandAction
Deferring breakpoint Jargoneer.commandAction.
It will be set after the class is loaded.
KVM_main[1] run
> Set deferred breakpoint Jargoneer.commandAction
Breakpoint hit: "thread=KVM_Threade702a0", Jargoneer.commandAction(), line=46 bc
i=0
KVM_Threade702a0[1] locals
Method arguments:
Local variables:
c = instance of javax.microedition.lcdui.Command(id=81965)
s = instance of javax.microedition.lcdui.TextBox(id=98361)
KVM_Threade702a0[1] dump c
c = {
SCREEN: 1
BACK: 2
CANCEL: 3
OK: 4
HELP: 5
STOP: 6
EXIT: 7
ITEM: 8
label: "Find"
commandType: 1
priority: 0
id: 4
commandRef: 6
}
KVM_Threade702a0[1]
In this session we just set a breakpoint in the commandAction() method, and when the debugger hits the breakpoint we tell it to print out the local variables and a dump of the object c. Notice the -connect argument specifies how to attach to the remote VM. These parameters are specified in the JDPA docs.
While a graphical debugger might be easier to use, jdb is installed with the Java development kit so it's readily available for some quick n' dirty debugging. If you're not using an IDE and need to debug a MIDlet, jdb can help you get the job done quickly.
Resources
Java Platform Debugger Architecture (JDPA)
http://java.sun.com/products/jpda/
http://java.sun.com/j2se/1.4/docs/guide/jpda/conninv.html
jdb Usage
http://java.sun.com/products/jpda/doc/jdb.html
Back To Top
|