This section provides a tutorial example on how to run a Java application and the 'jdb' debugger separately in socket mode. This allows you to debug Java applications remotely.
As we can see from the previous section, we can run a Java application and the 'jdb' debugger separately in shared memory mode.
But this requires that the Java application and the 'jdb' debugger must be running on the same machine.
If you have a large application that you need to run a big server machine, you can debug that application by running
the 'jdb' debugger on your own desktop machine using the socket debugging communication mode as presented in the previous section.
Let's try it now. Open a command window on a windows system and run:
C:\herong>java -agentlib:jdwp=transport=dt_socket,
address=localhost:8888,server=y,suspend=y Hello
Listening for transport dt_socket at address: 8888
The target application is launched in "socket server" mode.
Its execution is suspended. Now open another command window and run:
C:\herong>jdb -connect com.sun.jdi.SocketAttach:hostname=localhost,
port=8888
Set uncaught java.lang.Throwable
Set deferred uncaught java.lang.Throwable
Initializing jdb ...
>
VM Started: No frames on the current call stack
main[1] stop in Hello.main
Deferring breakpoint Hello.main.
It will be set after the class is loaded.
main[1] cont
> Set deferred breakpoint Hello.main
Breakpoint hit: "thread=main", Hello.main(), line=3 bci=0
3 System.out.println("Hello world!");
main[1] quite
Cool. I know how to run "jdb" to debug an application running on a remote machine now!
However, the command suggested in the JPDA documentation did not work:
C:\herong>jdb -attach localhost:8888
java.io.IOException: shmemBase_attach failed: The system cannot find
the file specified
at com.sun.tools.jdi.SharedMemoryTransportService.attach0(...
...
My guess is that the Windows version of "jdb" assumes "shared memory" as the default
transport mode.