Debugging a remote Java application

You may have debugged standalone Java applications from Eclipse. You set breakpoints in the code, run the application in the Debug mode from Eclipse, and then debug the application by stepping through the code. Debugging remote Java applications is a bit different, particularly when it comes to how you launch the debugger. In the case of local application, the debugger launches the application. In the case of remote application, it is already launched and you need to connect the debugger to it. In general, if you want to allow remote debugging for the application, you need to run the application using the following parameters:

-Xdebug -Xrunjdwp:transport=dt_socket,address=9001,server=y,suspend=n
  
  • Xdebug enables debugging
  • Xrunjdwp runs the debugger implementation of the Java Debug Wire Protocol (JDWP)

Instead of -Xdebug -Xrunjdwp, you can also use -agentlib:jdwp for JDK 1.5 and above, for example:

 -agentlib:jdwp=transport= dt_socket,address=9001,server=y,suspend=n

Let's understand the parameters used here in detail:

  • transport=dt_socket: This starts a socket server at address=9001 (this can be any free port) to receive debugger commands and send responses.
  • server=y: This tells the JVM if the application is a server or a client, in the context of debugger communication. Use the y value for remote applications.
  • suspend=n: This tells the JVM to not wait for the debugger client to attach to it. If the value is y, then the JVM will wait before executing the main class until a debugger client attaches to it. Setting the y value for this option may be useful in cases where you want to debug, for example, the initialization code of servlets that are loaded upon startup of the web container. In such cases, if you do not choose to suspend the application till the debugger connects to it, the code that you want to debug may get executed before the debugger client attaches to it.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.119.81