69

So i was going to debug my Solr filter plugins on Intellij Community Edition. After i ran the program from comand prompt with this command

java -jar start.jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8983

I started my Intellij debugger with this config:

Transport : socket
Debugger mode : attach
Host : localhost
Port : 8983

But when I ran the debugger I got this error:

Error running Debugger: Unable to open debugger port (localhost:8983): 
java.io.IOException "handshake failed - connection prematurally closed"

Any idea how to fix this?

llogiq
  • 13,815
  • 8
  • 40
  • 72
donthurtme
  • 1,347
  • 1
  • 10
  • 16

5 Answers5

76

I got that error when trying to access to debug port on a Docker container.

If you are trying to access the debug port inside a Docker container make sure you are specifying the port as *:5005

E.g.

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

This has been changes since Java 9.

See: REGRESSION: Remote debugging does not work on JDK 9

It's not a bug. It's a security.

Before the JDK-8041435

If you have a server with EXT and INT interfaces and start Java process with address=5900 it binds to both interfaces and allow anybody from entire world to connect to your java process unless you block it on firewall.

After JDK-8041435 socket transport try to guess localhost and bind to localhost only. I.e. socket transport by default works only if both client and server are located on the same machine. It's not an easy task to guess proper localhost. so ever same-machine configuration might not work in some situation because of network setup.

You can restore old, insecure behavior using * (asteric) i.e. -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5900 should work exactly as it was before JDK-8041435

But it's recommended to explicitly specify ip address to bind when it possible.

And JDWP socket connector accept only local connections by default

The JDWP socket connector has been changed to bind to localhost only if no ip address or hostname is specified on the agent command line. A hostname of asterisk (*) may be used to achieve the old behavior which is to bind the JDWP socket connector to all available interfaces; this is not secure and not recommended.

brass monkey
  • 5,841
  • 10
  • 36
  • 61
20

It should be something like this,

java "-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8983" -jar start.jar

it's working now

donthurtme
  • 1,347
  • 1
  • 10
  • 16
  • 14
    in intelli j 2019.2.4 , i am not able to edit command line arguments. How do i fix this ? my command line argument is - > -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:8787 and i keep on getting the below error -> Error running 'test': Unable to open debugger port (localhost:8787): java.io.IOException "handshake failed - connection prematurally closed" – Siyaram Malav Nov 06 '19 at 07:47
  • Just in case it helps someone, I had this error show up suddenly in Android Studio (based on Intellij) without me touching the build config at all. After fully shutting down and the starting the IDE again the error disappeared just as quickly as it appeared. – Ryan1729 May 21 '20 at 04:37
  • 1
    for people having problems updating the command line in Intelli idea, well you are not supposed to update the value in IntelliJ idea itself. You need to copy this value from the IntelliJ idea and add it in the java-options in tomcat if you are using tomcat or java-options or command line argument of your remote application. – skr Jan 25 '22 at 18:28
6

I had this error with OpenJDK 11 inside Docker container and setting environment variable JAVA_DEBUG_PORT to "*:5005" worked for me.

ant0nk
  • 134
  • 1
  • 7
4

You forgot to specify -Xdebug on the java command line.

Edit: As in

java -jar start.jar -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=8983
llogiq
  • 13,815
  • 8
  • 40
  • 72
  • 10
    It has everything to do with the java version. It doesn't do anything for Java >= 6. See this post: http://stackoverflow.com/questions/975271/remote-debugging-a-java-application#22631355 – Sam Sieber Dec 14 '16 at 20:18
1

It has helped me, at least in Intellij IDEA:

java -agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=0.0.0.0:5005

try to add ip 0.0.0.0.