In this section, try out the Java remote debugging stuff and record the simple things that get started. It's a record of use!
Write a simple program that hits a jar and drops it to the remote server to run, simulating remote server running. Take the Java call Shell script to submit the job program as an example analysis. The source code is as follows (the following program is a simple example code, do not care about coding specifications):
ImportJava.io.InputStream; Public classJavashell { Public Static voidMain (string[] args)throwsException {Try{String Grant= "chmod u+x submit-job.sh"; Runtime Runtime=Runtime.getruntime (); Process Grantproc=runtime.exec (Grant); intResultCode =grantproc.waitfor (); System.out.println (ResultCode); Grantproc= Runtime.exec ("./submit-job.sh"); ResultCode=grantproc.waitfor (); System.out.println (ResultCode); InputStream in=Grantproc.getinputstream (); byte[] buffer =New byte[1024]; intCode; while(Code = in.read (buffer, 0, buffer.length))! =-1) {System.out.print (NewString (buffer, 0, code)); } /*** Dead loop block debugger not connected on previous program exit (test suspend parameter function)*/System.out.println ("The shell script is finished and then the scheduled print task begins!" "); inti = 0; while(true) {Thread.Sleep (2000); System.out.println ("This is the" + (++i) + "secondary cycle! "); } } Catch(Exception e) {System.out.println ("This is a excption!"); } finally { } }}
After you commit the jar package to the remote server, run:
Java -xdebug-xnoagent-djava.compiler=none-xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=y -jar
You will find that the program is blocked and waiting for the debugger connection, at which point we can use Eclipse for remote debugging:
Click Debugger to connect, track the source code to run:
Remote has the output to prove that the remote program is tracking debugger execution:
By this we have completed the debugging process. Next, let's try the case of parameter suspend=n:
Java -xdebug-xnoagent-djava.compiler=none-xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n -jar
At this point, the remote program does not block waiting for the debugger to debugger the connection, but the program executes normally:
At this point we can use the remote debugger of Eclipse for remote debugging, but note that our local breakpoints can only be hit at the point where the remote has not executed the code or where the code is being executed, for example we can break points in the dead loop (the dead loop is the code that has been executing):
Then after starting debugger:
Stay at the breakpoint. Then follow the breakpoint location to continue our debug work. You should be able to know what the suspend parameter does:
In the JVM debug parameter, there is a parameter called "suspend", which has a value of two, "Y" or "n".
If you want to debug at first, set the parameter to "Suspend=y" so that eclipse will connect to the Java application remotely.
If you want to run the project first and then connect to eclipse, you can set the parameter to "Suspend=n" so that the Java application will run normally and then eclipse will start the remote connection.
More parameter details:
-xdebug enable debugging.
-xnoagent disables the default Sun.tools.debug debugger.
-djava.compiler=none suppresses the loading of the JIT compiler.
-XRUNJDWP loads the JDWP JPDA reference execution instance.
Transport is used to communicate between the debugger and the process used by the VM.
Dt_socket socket transmission.
Dt_shmem shared memory transfers are limited to Windows.
server=y/n whether the VM needs to be executed as a debug server.
address=3999 the port number of the debug server that the client uses to connect to the server.
suspend=y/n whether to start the VM after the debugging client establishes a connection.
Reference: http://calvinliu.iteye.com/blog/876009
Java about the Remote Debugging program tutorial