In this section, try Java remote debugging and record simple things to get started. Even if it is a record!
Write a simple program and type it into a jar and throw it into a remote server to run, simulating that the remote server is running. Take Java calling shell script submission job program as an example. The source code is as follows (the following program is a simple example code, don't care about the code specification):
import java.io.InputStream;public class JavaShell { public static void main(String[] args) throws Exception { try { String grant = "chmod u+x submit-job.sh"; Runtime runtime = Runtime.getRuntime(); Process grantProc = runtime.exec(grant); int resultCode = 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]; int code; while ((code = in.read(buffer, 0, buffer.length)) != -1) { System.out.print(new String(buffer, 0, code)); } /** * The dead loop prevents the debugger from exiting before it is connected (test the suspend parameter function) */ System.out.println("Shell script has been executed, and the timed printing task will start!"); int i = 0; while (true) { Thread.sleep(2000); System.out.println("This is the "th" + (++i) + "second loop!"); } } catch (Exception e) { System.out.println("this is a excption!"); } finally { } }}After committing it to the remote server, run it:
Copy the code as follows: java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=y -jar JavaShell.jar
The program will be blocked and wait for the debugger to connect. At this time, we can use eclipse for remote debugging:
Click debugger to connect and track the source code to run:
There is output remotely, proving that the remote program is tracking debugger execution:
At this point, we complete the debugging process. Next, try the case where the parameter suspend=n:
Copy the code as follows: java -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,address=9999,server=y,suspend=n -jar JavaShell.jar
At this time, you will find that the remote program does not block and wait for the debugger debugger connection, but the program executes normally:
At this time, we can use the remote debugger of eclipse for remote debugging, but it should be noted that our local breakpoint can only be hit at the remote code that has not been executed or the code that is being executed. For example, we can break points in the dead loop (the dead loop is the code that is being executed all the time):
Then start debugger:
Stay at the breakpoint. Then follow the breakpoint position to continue our debug work. At this point, you should know the role of the suspend parameter:
In the JVM DEBUG parameters, there is a parameter called "suspend", which has two values, "y" or "n".
If you want to debug at the beginning, set the parameter to "suspend=y", so that Eclipse will remotely connect to Java applications.
If you want to run the project first and then connect Eclipse, you can set the parameter to "suspend=n", so that the Java application will run normally, and then Eclipse will start remote connection.
More parameter details:
-XDebug Enable debugging.
-Xnoagent Disable the default sun.tools.debug debugger.
-Djava.compiler=NONE Prohibits loading of the JIT compiler.
-Xrunjdwp Loads JPDA reference execution example of JDWP.
transport is used to communicate between debuggers and processes used by VMs.
dt_socket socket transfer.
dt_shmem Shared memory transfer, Windows only.
server=y/n Whether the VM needs to be executed as a debugging server.
address=3999 The port number of the debug server, the port number used by the client to connect to the server.
suspend=y/n Whether to start the VM after the debug client establishes a connection.
Eclipse debug shortcut keys:
F5 Step into
F6 Step over
F7 Step out
F8 continue to the next breakpoint
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.