Remote debugging a Java application

438,961

Solution 1

Edit: I noticed that some people are cutting and pasting the invocation here. The answer I originally gave was relevant for the OP only. Here's a more modern invocation style (including using the more conventional port of 8000):

java -agentlib:jdwp=transport=dt_socket,server=y,address=8000,suspend=n <other arguments>

Original answer follows.


Try this:

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

Two points here:

  1. No spaces in the runjdwp option.
  2. Options come before the class name. Any arguments you have after the class name are arguments to your program!

Solution 2

For JDK 1.3 or earlier :

-Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=6006

For JDK 1.4

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

For newer JDK :

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

Please change the port number based on your needs.

From java technotes

From 5.0 onwards the -agentlib:jdwp option is used to load and specify options to the JDWP agent. For releases prior to 5.0, the -Xdebug and -Xrunjdwp options are used (the 5.0 implementation also supports the -Xdebug and -Xrunjdwp options but the newer -agentlib:jdwp option is preferable as the JDWP agent in 5.0 uses the JVM TI interface to the VM rather than the older JVMDI interface)

One more thing to note, from JVM Tool interface documentation:

JVM TI was introduced at JDK 5.0. JVM TI replaces the Java Virtual Machine Profiler Interface (JVMPI) and the Java Virtual Machine Debug Interface (JVMDI) which, as of JDK 6, are no longer provided.

Solution 3

Steps:

  1. Start your remote java application with debugging options as said in above post.
  2. Configure Eclipse for remote debugging by specifying host and port.
  3. Start remote debugging in Eclipse and wait for connection to succeed.
  4. Setup breakpoint and debug.
  5. If you want to debug from start of application use suspend=y , this will keep remote application suspended until you connect from eclipse.

See Step by Step guide on Java remote debugging for full details.

Solution 4

Answer covering Java >= 9:

For Java 9+, the JVM option needs a slight change by prefixing the address with the IP address of the machine hosting the JVM, or just *:

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

This is due to a change noted in https://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435.

For Java < 9, the port number is enough to connect.

Solution 5

I'd like to emphasize that order of arguments is important.

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 -jar app.jar command opens debugger port,

but java -jar app.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=8000 command doesn't. It will pass everything after app.jar as command-line arguments.

Share:
438,961

Related videos on Youtube

Admin
Author by

Admin

Updated on June 19, 2021

Comments

  • Admin
    Admin almost 3 years

    I have a java application running on linux machine. I run the java application using the following:

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

    I have opened port 4000 for TCP on this Linux machine. I use eclipse from Windows XP machine and try to connect to this application. I have opened the port in windows also.

    Both machines are on the LAN but I can't seem to connect the debugger to the Java application. What am I doing wrong?

  • Mat Gessel
    Mat Gessel about 10 years
  • C. K. Young
    C. K. Young almost 9 years
    @DJGummikuh Nice! I've updated the post to use the newer-style -agentlib option for your cutting-and-pasting pleasure. :-)
  • Nathan Niesen
    Nathan Niesen over 5 years
    In Java 8 the JDK supports a JAVA_TOOL_OPTIONS environment variable so to enable the debugger for any Java application you need to use: JAVA_TOOL_OPTIONS=-agentlib:jdwp=transport=dt_socket,address‌​=8000,server=y,suspe‌​nd=n p.s. sorry for the edits, fighting with the formatter.
  • Marvin
    Marvin over 5 years
    Worth repeating a comment from this stackoverflow.com/a/138518/500902, "Since Java 9 "address=1044" is not always listening on all interfaces. "address=*:1044" makes Java 9+ behave like Java 8" to allow debugging from different host
  • xoX Zeus Xox
    xoX Zeus Xox over 4 years
    I would guess that is because in your second example, everything after "app.jar" is passed as arguments into your main method
  • MrBlack
    MrBlack over 4 years
    @xoXZeusXox ha ha. Yes, it passed as arguments. Thanks for mentioning.
  • Joshua Goldberg
    Joshua Goldberg over 3 years
    The "Step by Step guide" in the link uses some out-of-date options.
  • petronius
    petronius over 3 years
    That seems new to java 11
  • Martynas Jusevičius
    Martynas Jusevičius over 3 years
    Any idea about NetBeans?