388

I know there's some JAVA_OPTS to set to remotely debug a Java program.

What are they and what do they mean ?

Gama11
  • 31,714
  • 9
  • 78
  • 100
paulgreg
  • 18,493
  • 18
  • 46
  • 56

9 Answers9

515

Before Java 5.0, use -Xdebug and -Xrunjdwp arguments. These options will still work in later versions, but it will run in interpreted mode instead of JIT, which will be slower.

From Java 5.0, it is better to use the -agentlib:jdwp single option:

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

Options on -Xrunjdwp or agentlib:jdwp arguments are :

  • transport=dt_socket : means the way used to connect to JVM (socket is a good choice, it can be used to debug a distant computer)
  • address=8000 : TCP/IP port exposed, to connect from the debugger,
  • suspend=y : if 'y', tell the JVM to wait until debugger is attached to begin execution, otherwise (if 'n'), starts execution right away.
Gergely Toth
  • 6,638
  • 2
  • 38
  • 40
paulgreg
  • 18,493
  • 18
  • 46
  • 56
  • 7
    Your are exactly correct.. I tried with **-Xdebug** and **-Xrunjdwp** but It didn't work. when I tried with **-Xrunjdwp** or **agentlib:jdwp** It is working. ( on Java 7 ) – RoboAlex Jun 01 '12 at 06:02
  • This only worked for me when I put quotes around it, otherwise I got this error: ERROR: JDWP Non-server transport dt_socket server=y suspend=y address=8000 must have a connection address specified through the 'address=' option – Ring Apr 21 '16 at 15:19
  • 2
    Is `Xrunjdwp` deprecated (or removed?) why would we pick `agentlib:jdwp` over it? – ArtOfWarfare Apr 29 '16 at 17:08
  • 2
    I think you're wrong. Netbeans uses `-Xdebug -Xrunjdwp` when debugging Maven projects, and they run like they're JITed. – Aleksandr Dubinsky May 30 '16 at 13:47
372

I have this article bookmarked on setting this up for Java 5 and below.

Basically run it with:

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

For Java 5 and above, run it with:

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

If you want Java to wait for you to connect before executing the application, replace suspend=n with suspend=y.

izogfif
  • 6,000
  • 2
  • 35
  • 25
Hans Sjunnesson
  • 21,745
  • 17
  • 54
  • 63
  • 42
    Worth to mention is that you might want to configure your address like `*:1044` to enable remote debugging from any computer – Herr Derb Aug 24 '18 at 11:58
  • 9
    HerrDerb is right.. Since Java 9 "address=1044" is not always listening on all interfaces. "address=*:1044" makes Java 9+ behave like Java 8... – alfonx Sep 05 '18 at 13:47
  • 4
    Just a note: It is _**not required**_ to add the `address` parameter. If not provided the agent is selecting a random port number. This might be useful if you start multiple nodes within the same java command line. – asbachb Jul 01 '20 at 14:57
  • 4
    its also safer to use `address=localhost:` vs just selecting the port – Decoded Nov 05 '20 at 23:28
115

Since Java 9.0 JDWP supports only local connections by default. http://www.oracle.com/technetwork/java/javase/9-notes-3745703.html#JDK-8041435

For remote debugging one should run program with *: in address:

-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8000
Antony Shumskikh
  • 1,251
  • 1
  • 8
  • 7
  • 3
    Just making a note: the Oracle link says this will achieve the same old behavior, but it is not secure and not recommended. – Chanseok Oh Mar 07 '19 at 18:41
  • Yeah, I wasted some days to find this answer for docker-compose, docker swarm debug remote with inteliji, just do exactly(with `*:`): address=*:5005 – nobjta_9x_tq Nov 12 '21 at 19:01
  • new non-dead link: https://www.oracle.com/java/technologies/javase/9-all-relnotes.html – starwarswii Sep 28 '22 at 15:23
  • In general I think it would be better (more secure, and generally better practice) to bind the port to localhost, and set up an ssh tunnel. – Rory Browne Jul 28 '23 at 08:56
13

For java 1.5 or greater:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 <YourAppName>

For java 1.4:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 <YourAppName>

For java 1.3:

java -Xnoagent -Djava.compiler=NONE -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=5005 <YourAppName>

Here is output from a simple program:

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=1044 HelloWhirled
Listening for transport dt_socket at address: 1044
Hello whirled
thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30
10

java

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8001,suspend=y -jar target/cxf-boot-simple-0.0.1-SNAPSHOT.jar

address specifies the port at which it will allow to debug

Maven

**Debug Spring Boot app with Maven:

mvn spring-boot:run -Drun.jvmArguments=**"-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8001"
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Jovi Qiao
  • 101
  • 1
  • 3
4

Command Line

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

Gradle

gradle bootrun --debug-jvm

Maven

mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=PORT_NUMBER
BuZZ-dEE
  • 6,075
  • 12
  • 66
  • 96
Santosh b
  • 729
  • 1
  • 8
  • 19
2

Here is the easiest solution.

There are a lot of environment special configurations needed if you are using Maven. So, if you start your program from maven, just run the mvnDebug command instead of mvn, it will take care of starting your app with remote debugging configurated. Now you can just attach a debugger on port 8000.

It'll take care of all the environment problems for you.

neves
  • 33,186
  • 27
  • 159
  • 192
1
-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=PORT_NUMBER

Here we just use a Socket Attaching Connector, which is enabled by default when the dt_socket transport is configured and the VM is running in the server debugging mode.

For more details u can refer to : https://stackify.com/java-remote-debugging/

mate00
  • 2,727
  • 5
  • 26
  • 34
Boney
  • 1,462
  • 1
  • 11
  • 19
-1

If you are using java 9 or higher, to remotely debug (which is also the case when you use docker at local) you have to provide --debug *:($port). Because from java 9 --debug ($port) will only allow to debug at local, not remotely.

So, you can provide command in docker-compose like command: -- /opt/jboss/wildfly/bin/standalone.sh --debug *:8787