7

I am trying to setup a remote debugger in Intellij v2020.1, I keep getting the error below:

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

In my docker compose file I have mounted port 5005 to 5005

In my docker file i have:

EXPOSE 5005

ENTRYPOINT ["/bin/bash", "runme.sh"]

and in my shell script I have:

/opt/java/openjdk/bin/java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -jar mine.jar

When I do a docker ps, i can see the below:

0.0.0.0:5005->5005/tcp, 0.0.0.0:8111->8111/tcp

In IntelliJ I setup the remote debugger from port 5005 to contaier port 5005, added in the module claspath and in the before launch step, added in my compose file.

The service starts up fine, but jut cant connect to the debugger, any ideas?

CrazyCoder
  • 389,263
  • 172
  • 990
  • 904
qwertyqwerty
  • 207
  • 5
  • 15
  • Does `jdb -attach 5005` work? – CrazyCoder Jul 16 '20 at 21:24
  • where do I place that? – qwertyqwerty Jul 16 '20 at 22:37
  • 2
    `jdb` is the command line debugger provided with the JDK, you run this command in the terminal on the host system. See also https://www.tutorialspoint.com/jdb/jdb_quick_guide.htm. This will help to understand if the issue has anything to do with IntelliJ IDEA. In case `jdb` also fails, then the issue is with some networking configuration between the host and Docker. Also, what Java version do you use in Docker? If it's Java 9+, the command line to enable debugging should look like this: `-agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005` so that it binds on all interfaces. – CrazyCoder Jul 16 '20 at 22:42
  • Thanks for the reply, tried this on the host system and also get the same error, so its not an IntelliJ issue. I am using Jdk8 – qwertyqwerty Jul 17 '20 at 08:16
  • Also tracked at https://youtrack.jetbrains.com/issue/IDEA-173607 with no solution found so far. – CrazyCoder Jul 17 '20 at 08:18
  • I switched the port to something else, and it has started working. Thanks for the help – qwertyqwerty Jul 17 '20 at 08:44
  • agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 works for OpenJDK 11. Thanks! – Eugene Chung Dec 08 '20 at 13:50
  • @CrazyCoder I assume jdb -attach 5005 should work? I get Unable to attach to target VM. – Darius.V Apr 11 '22 at 11:50
  • https://www.jetbrains.com/help/idea/run-and-debug-a-spring-boot-application-using-docker-compose.html#53c9c47c just tested from this - jdb -attach 5005 gives exception but the code is stopped at breakpoints in demo application. – Darius.V Apr 11 '22 at 12:25

1 Answers1

17

Placing this line in my Dockerfile solved it for me:

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

The critical part here, being the "address=*:5005" and not simply "address=5005". Apparently some security changes since Java 9 requires the *: before the port.

EDIT: I see you're on Java 8. But perhaps adding -XDebug to the options as well, could do the trick:

ENV JAVA_TOOL_OPTIONS -Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005
radrocket81
  • 299
  • 3
  • 10