Remote debugging of Java programs

Source: Internet
Author: User

Java can be started in debug mode. After it is started, you can use jdb to debug the program. To start in debug mode, you must add the following parameters:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n

Generally, JAVA_OPTS environment variables are used on servers such as tomcat, jboss, and weblogic. To run such services, you can set the environment variables as follows:

Linux

export JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"JAVA_OPTS="$JAVA_OPTS -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n"

Windows

set JAVA_OPTS=%JAVA_OPTS% -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n

Maven uses the MAVEN_OPTS parameter to set the same as above

The following is a simple server code that receives UDP requests.

 ; ..; ..; ..;   {     ([] )   {      =  (8000);     () {        =  ( [100], 100);      socket.receive(dataPackage);      System.out.println(          + dataPackage.getAddress().getHostName());      System.out.println( +  (dataPackage.getData()) + );      dataPackage.setData(.getBytes());      socket.send(dataPackage);    }  }}

Run the following command to run the server:

java -Xdebug -Xrunjdwp:transport=dt_socket,address=8787,server=y,suspend=n mm.UServer

Run jdb with the following fate to perform debugging:

[Root @ server test] # jdb-attach 192.168.1.79: 8787-sourcepath. set uncaught java. lang. throwableSet deferred uncaught java. lang. throwableInitializing jdb...> stop at mm. UServer: 12 Set breakpoint mm. UServer: 12> Breakpoint hit: "thread = main", mm. UServer. main (), line = 12 bci = 1112 DatagramPacket dataPackage = new DatagramPacket (new byte [100], 100); main [1] nextStep completed: "thread = main", mm. UServer. mai N (), line = 13 bci = 2513 socket. receive (dataPackage); main [1] main [1] next> nextNothing suincluded.> step completed: "thread = main", mm. UServer. main (), line = 14 bci = 3014 System. out. println ("Receive from:" main [1]! NextStep completed: "thread = main", mm. UServer. main (), line = 15 bci = 4215 + dataPackage. getAddress (). getHostName (); main [1] main [1]! Next> Step completed: "thread = main", mm. UServer. main (), line = 14 bci = 5514 System. out. println ("Receive from:" main [1]! Next> Step completed: "thread = main", mm. UServer. main (), line = 16 bci = 5816 System. out. println ("Data: [" + new String (dataPackage. getData () + "]"); main [1]! Next> Step completed: "thread = main", mm. UServer. main (), line = 17 bci = 9517 dataPackage. setData ("abc hello ". getBytes (); main [1]! Next> Step completed: "thread = main", mm. UServer. main (), line = 18 bci = 10418 socket. send (dataPackage); main [1]! Next> Step completed: "thread = main", mm. UServer. main (), line = 11 bci = 10911 while (true) {main [1]! Next> Step completed: Breakpoint hit: "thread = main", mm. UServer. main (), line = 12 bci = 1112 DatagramPacket dataPackage = new DatagramPacket (new byte [100], 100); main [1]

Main Debugging commands include:

  • Stop
  • Stop on sets the breakpoint in the specified Method
  • Next
  • !! Repeat the last command

Currently, jdb has been integrated in eclipse. You can set the debug option of eclipse to connect to a remote host for remote debugging.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.