Wednesday, August 15, 2012

Remote Debug Your Application in Jetty on Remote Machine

Motivation: Something weird happened today, I was asked to setup remote debug mechanism on our test server so that developers can step through code in their machine while execution takes place on test environment sitting in other city.

I never imagined need of remote debug to actually debug a code on remote machine.

Prerequisite: 
  1. Standalone Jetty installation
  2. Eclipse (or your favorite Java IDE that has remote debug facility)
Steps: $JETTY_HOME denotes the directory where Jetty is installed/copied/extracted.

Edit ini file: Open $JETTY_HOME/start.ini file in your favorite text editor. uncomment --exec and after that add the debug setup. Here is a snippet of that file:
--exec
-Xmx1194m
-Xmn256m
-Xdebug
-Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n
# -verbose:gc
Uncommenting --exec basically makes content of this file gets passed as VM parameters when Jetty is launched. Which is nothing special -- in bare minimum form this is what gets executed:
java -Xdebug -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n -jar start.jar

Start Jetty as usual: start Jetty by $JETTY_HOME/bin/jetty.sh start

How do I confirm if these parameters are passed? If you are on Unix, just run ps -ef | grep  -v grep | grep jetty, you will see long description with the parameters above in it:
root     30320 30308  0 17:53 pts/0    00:00:07 /usr/java/jdk1.6.0_30/jre/bin/java -Xmx1194m -Xmn256m \
-Xdebug -Xrunjdwp:transport=dt_socket,address=4000,server=y,suspend=n \
 -Djetty.home=/opt/apps/jetty-hightide-7.5.0.v20110901 -cp \
/opt/apps/jetty-hightide-7.5.0.v20110901/lib/jetty-xml-7.5.0.v20110901.jar: \
/opt/apps/jetty-hightide-7.5.0.v20110901/lib/servlet-api-2.5.jar: \
/opt/apps/jetty-hightide-7.5.0.v20110901/lib/jetty-http-7.5.0.v20110901.jar:
...
Connecting to Remote Debugger: In Eclipse, go to Run > Debug Configurations... menu. Double click Remote Java Application, give an appropriate name, link to relevant project, provide the host-name, mention the port that you have started your remote debugger on. So, from step#1, in my case this will be 4000.

Here is the screen shot:

Apply and start debugging.

A word about firewall: Since it makes TCP connection to your Jetty make sure, you have debug port -- 4000, open on the host machine. In AWS EC2, you can allow port 4000 in your security group.

3 comments:

  1. This is EXACTLY what I needed!! Thank you!
    I did have to open a firewall port, since I was on Linux I had to edit the IPtables with this command

    ReplyDelete
  2. You can always forward a port with ssh, this way you don't need to open a port in your firewall.

    Like this:
    ssh -L 5000:127.0.0.1:4000 user@remotehost

    This way you will bind your local port 5000 to the remote address 127.0.0.1 (from the remote machine point of view) at the port 4000. And you will have to configure eclipse to point to localhost:5000

    ReplyDelete