miércoles, 8 de mayo de 2013

Debugging Stand-alone Java applications

When developing applications running in standalone servers, we usually find ourselves guessing the nature of the bugs and exceptions we get. That's when we miss the debugger.
Although is well know that most standalone Java applications are debuggeable, when running outside the context of our IDE it seems quite mysterious the way to do it.
So, my coworker had spent some time researching the matter and told me the solution.
We were running a CQ5.5 WCM stand alone jar, and we wanted to debug the OSGi servlets we were developing for it (CQ has an Apache Felix OSGi container within).

java -Duser.country=AR -Duser.timezone=America/Argentina/Buenos_Aires -Dsun.lang.ClassLoader.allowArraySyntax=true -XX:MaxPermSize=256m -XX:+UseParallelGC -Xmn128m -Xms512m -Xmx1024m -Xss2m -agentlib:jdwp=transport=dt_socket,address=localhost:30303,server=y,suspend=n -jar cq55-author-4502.jar -gui -p 4502 -nofork

The most important parameters are those I put in bold.
-agentlib:jdwp=transport=dt_socket,address=localhost:30303,server=y,suspend=n
From this line we should look at "localhost:30303" which is the address/port where the JVM will be listening for debuging.

In the other hand we'll have to configure our IDE to debug that standalone server. I was using Eclipse for that. You'll have to get into the "Debug Configurations" dialog (just look at the debug button drop-down menu). There you'll have to take care for the following config parameters.
  • Name: some name for you to identify what are you going to debug when you press the Debug button in your Eclipse
  • Project: the project you want to debug
  • Host/Port: remember that string address=localhost:30303? Exactly!
  • Source tab: just check that your sources are there. If any source is missing just add it there.
 And that's all folks!

No hay comentarios:

Publicar un comentario