Remote debugging a Java application [duplicate]
Asked Answered
B

8

301

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?

Blouse answered 10/6, 2009 at 12:27 Comment(1)
M
532

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>

Note: With address=8000 (as used above), the debug server will only listen on localhost (see What are Java command line options to set to allow JVM to be remotely debugged?). If you want the server to listen on all interfaces, to be able to debug across the network, use address=*:8000. Obviously only do this on a restricted, trusted network...

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!
Matthiew answered 10/6, 2009 at 12:47 Comment(11)
Official JDPA Connection and Invocation DocumentationDelatorre
@DJGummikuh Nice! I've updated the post to use the newer-style -agentlib option for your cutting-and-pasting pleasure. :-)Matthiew
Do we always need to have the source code for the remote app to be present in the machine where we do remote debugging ?Fujimoto
You need to know the source code. Either you have the .java files or you have the .jar / .class files combined with the decompiler. IDE such as Eclipse can have a decompiler such as JDecompiler installed so that you can debug the .class file as if it's a .java file (excluding the comments).Impending
what about java9+ applications optimized with jlink?Ascogonium
How can we run shell script by passing the below parameters ?Tunny
What if the java process is not started with thoses arguments int the first place? Can I somehow make the said jvm instance debuggable?Termless
Worth repeating a comment from this https://mcmap.net/q/86576/-what-are-java-command-line-options-to-set-to-allow-jvm-to-be-remotely-debugged, "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 hostEnthusiast
Thanks. I'd like to emphasize that order of arguments is important, for me java -jar app.jar -agentlib command didn't open debugger port, but java -agentlib -jar app.jar didCosma
Suggested updated to this answer.... I had to modify this solution to work for connecting to java on a windows machine with multiple network interfaces. Only setting address=*:4000 allowed me to connect to the java process from my IDE on another computer. My final working solution is '-Xdebug Xrunjdwp:server=y,transport=dt_socket,address=*:4000,suspend=n'.Conflux
anybody else has been coming back to this page for years to copy/paste?Plumlee
L
95

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.

Lucubration answered 25/3, 2014 at 10:12 Comment(1)
The following works with Eclipse's default settings: -agentlib:jdwp=transport=dt_socket,server=y,address=8000Readytowear
M
49

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/java/technologies/javase/9-all-relnotes.html#JDK-8041435.

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

Mimicry answered 30/6, 2019 at 9:23 Comment(3)
The page attached doesn't exitGeny
@VijayanKani Thanks I searched and found an updated page.Mimicry
Quoting from the release notes you linked regarding *: this is not secure and not recommended; so you need additional measures to secure the connection, such as SSH (?), see also "Is it safe to expose java remote debugger port to the internet?"Idiographic
D
33

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.

Dy answered 15/11, 2011 at 4:35 Comment(1)
The "Step by Step guide" in the link uses some out-of-date options.Greenwell
C
16

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.

Cosma answered 8/9, 2019 at 14:18 Comment(3)
I would guess that is because in your second example, everything after "app.jar" is passed as arguments into your main methodUnderpay
@xoXZeusXox ha ha. Yes, it passed as arguments. Thanks for mentioning.Cosma
That seems new to java 11Quincuncial
N
3

This is how you should setup Eclipse Debugger for remote debugging:

Eclipse Settings:

1.Click the Run Button
2.Select the Debug Configurations
3.Select the “Remote Java Application”
4.New Configuration

  • Name : GatewayPortalProject
  • Project : GatewayPortal-portlet
  • Connection Type: Socket Attach
  • Connection Properties: i) localhost ii) 8787

For JBoss:

1.Change the /path/toJboss/jboss-eap-6.1/bin/standalone.conf in your vm as follows: Uncomment the following line by removing the #:

JAVA_OPTS="$JAVA_OPTS -agentlib:jdwp=transport=dt_socket,address=8787,server=y,suspend=n"

For Tomcat :

In catalina.bat file :

Step 1:

CATALINA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8000,server=y,suspend=n"

Step 2:

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

Step 3: Run Tomcat from command prompt like below:

catalina.sh jpda start

Then you need to set breakpoints in the Java classes you desire to debug.

Nimocks answered 28/6, 2016 at 22:48 Comment(2)
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,suspend=n p.s. sorry for the edits, fighting with the formatter.Kerr
Any idea about NetBeans?Nanananak
U
2

for everybody that has the problem that if you really do remote debugging from 1 machine to the other then using :

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

is not enough because that binds now (at least on unix/osx machines) to localhost so you can only connect to it from localhost.

If you try to remote debug this then you will get connection refused for this. From i think Java 9 on you need to do:

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

or give an ip that it needs to bind on for hat *

Uball answered 18/6, 2021 at 12:55 Comment(0)
C
0

Every solution here is describing the method to attach debugger after the application starts. In the "debugger listening" setup you need to set server=n and suspend=n. If not the Gradle will throw permission denied error.

build.gradle:

run {
    jvmArgs = ["-agentlib:jdwp=transport=dt_socket,server=n,suspend=n,address=5005"
}
Calcareous answered 26/10, 2023 at 14:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.