28

I've got a server running on DigitalOcean and a JAR file that I want to debug. I first start the JAR on the remote server using

java -jar Server.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005

but on the console I see no output like "listening on port 5005...".

When I press debug in IntelliJ it says

unable to open debugger port (198.xxx.xxx.xx:5005): java.net.ConnectException "Connection refused"

This is my IntelliJ configuration:
enter image description here

I also tried using -Xdebug but it still didn't work.

If I set suspend=y it should wait until a debugger is connected, but instead, it starts without problems.

SOLO
  • 868
  • 9
  • 19
Leonardo
  • 499
  • 1
  • 7
  • 18

11 Answers11

19

The command to start the remote Java process in debug mode looks correct. If you don't see "Listening to Port blah" when you start the server JAR, then it might mean that the debug args are not being picked up. Another way to quickly check this would be to test with a telnet localhost 5005 on the machine where the server JAR is being executed. The telnet will fail if that port is not being used.

I suggest that you try the following, since the order of the parameters might be significant (I'll add some official evidence for this later):

java "agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" -jar Server.jar
SOLO
  • 868
  • 9
  • 19
Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
15

this command worked for me:

export JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5005'

by default idea remote dialog suggest:

'agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005'

change it to:

'agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5005'

and issues port 5005.

Rafael Reyes
  • 2,615
  • 8
  • 34
  • 51
suifengpiao14
  • 159
  • 1
  • 2
  • 1
    It worked for me, but I don't know why... Problem was with intellij connecting to tomcat inside docker container. – mbartn Oct 30 '19 at 10:41
  • While connecting to remote server from Intellij IDEA, when I used export JAVA_OPTS='-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5505' it didn't work. But when I used address=0.0.0.0:5505 then it worked. – K. Siva Prasad Reddy May 30 '20 at 04:23
  • 2
    Lifesaver! Thank you. I spent last hour trying different configurations and all I was missing was "0.0.0.0" part before port – Lukenzo Jan 17 '21 at 18:46
  • This in setenv '-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5505' work for me – abinthomas12914 Nov 17 '21 at 07:55
  • Adding "0.0.0.0:" makes it work! Thanks! But what's the difference did "0.0.0.0:" make? – Instein Feb 13 '22 at 07:26
4

This command worked for me:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar Server.jar 
martijnn2008
  • 3,552
  • 5
  • 30
  • 40
2

Thanks to suifengpiao14 but I'm going to describe the problem a bit more in detail.

I checked multiple things, and at the end found the reason: actually as like as a restful service we want to be accessible from out of the server we are running it that we should set 0.0.0.0 as the nameserver, here we should do a similar one. I checked the difference between the server from which I can remotely debug and the one which I can't. using netstat command:

for the server which I was ok with:

tcp        0      0 0.0.0.0:5005            0.0.0.0:*               LISTEN      8323/java

for the server which I had problem with:

tcp        0      0 127.0.0.1:5005            0.0.0.0:*               LISTEN      8323/java

So, using below jvm options should be helpful:

'agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005'
'agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=0.0.0.0:5005'
0

In my case it was because under settings -> build, execution, deployment -> debugger I had a built in server running on the same port as which I was trying to attach my debugger to for some reason.

jacob
  • 398
  • 4
  • 13
0

For people like me who sometimes forget to read...

Copy and paste the arguments to the command line when JVM is started
says the Run/Debug Configuration in IntelliJ, directly under:
-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

Means: Copy this line and go to your docker configuration. Add an environment variable (modify options dropdown). Paste it there with JAVA_OPTS= prepended.

Now when you did every correctly, you will have
Listening for transport dt_socket at address: 5005
and
Command line argument: -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005

This is who I solved it...

luke8800gts
  • 398
  • 3
  • 7
0

I've reproduced similar issue:

Unable to open debugger port (localhost:5005): java.net.ConnectException "Connection refused (Connection refused)" 

I had it while running debugger w/ command line arguments for remote JVM using Run/Debug configurations:

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

by attaching IDE with exposed debug agent server.

Since I've used Kubernetes cluster, the reason was:

once the pod has been created and was running as expected, I needed to proceed to set up port forwarding with the kubectl port-forward command, with the port to expose locally for traffic from the application before running debugger.

Based on kubectl port-forward syntax:

kubectl port-forward <resource-type/resource-name> [local_port]:<pod_port>

In format like:

kubectl port-forward <pod-name> -n <namespace> 5005:5005

or in a shorter form:

kubectl port-forward <pod-name> 5005:5005

The client listens on port 5000 locally and forwards to 5000 in the pod.

Accordingly entrypoint.sh file of the service was configured w/ the following command for Kubernetes cluster:

java -Djava.security.egd.=file:/dev/./urandom -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 -Xmx400m -jar /service.jar

As result, running port-forward before debugger solved the issue.

invzbl3
  • 5,872
  • 9
  • 36
  • 76
0

One of the reason is that the port is not enabled. You can try by enabling the port using the below command

firewall-cmd --zone=public --permanent --add-port=8001/tcp

Once the port is up, restart tomcat & try connecting again. Hope it helps.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
0

This might help someone That port in JVM debug is not your web app port

Bawantha
  • 3,644
  • 4
  • 24
  • 36
0

When running debugger port in tomcat, you should start the tomcat server with this command:

catalina.bat jpda start
0

this solved it for me:

net stop winnat

net start winnat

IntelliJ IDEA - Address localhost:1099 is already in use

womd
  • 3,077
  • 26
  • 20