handshake failed error with Remote Debugging

Answered

Hi,
When I try remote debugging on my mac it work without any problem but when I'm try it from on local vagrant box machine i got the following exception in the event log

Error running retailapp: Unable to open debugger port : java.io.IOException "handshake failed - connection prematurally closed"

i used the following command to run the jar on the local vagrant box

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

That is after i have made prot forwarding for the fort 5005 in the Vagrantfile

    override.vm.network "forwarded_port", guest: 5005, host: 5005, protocol: 'tcp' # InteliJ Debugger

and used the following remote configuration on IntelliJ

Screen Shot 2014-06-24 at 4.29.47 PM.png
4
28 comments
Avatar
Permanently deleted user

Hi Everyone,

 

Did you solve this problem? I've got same error now. If you solved, could you please help me to solve.

 

Thank you

 

 

0

The error you described is completely different: 

The initial error: 

"handshake failed - connection prematurally closed"

Your error: 

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

In your case, remote debugger needs to connect to the JVM that is already running in debug mode. Make sure you start the app you want to debug first with the JVM options suggested by the IDE. Then start Remote configuration to connect and debug this app.

Also, make sure that localhost is configured in your hosts file like that: 

127.0.0.1 localhost
0
Avatar
Permanently deleted user

Hi Konstantin, 

Thank you for your kind reply.

You're right. When I choose port address other than 5005 I've got above error. But when I used 5005 I've got following error. 

 

 

 

0
Avatar
Permanently deleted user

No! it is not inside Docker.

0

May you show the command how the application is started?

0
Avatar
Permanently deleted user

I am using debug button instead command for starting application. 

When I clicked debug button I've got error with above settings. 

0

This configuration (RemoteDebug) is used to connect to an already running process. In case you want to start an application in debug mode, please use "Application" Run/Debug configuration: 

https://www.jetbrains.com/help/idea/debugging-your-first-java-application.html

0
Avatar
Permanently deleted user

Konstantin,

Really, I appreciate for your patience.

I can give info briefly about my project and what we wanted to do. We have project running on openshift and I want to debug that project via IntelliJ. When I search on the Internet, I've found above settings to apply and I applied. But I've got error as we've mentioned before.

 

Thank you again.

 

 

 

0

Ah, I see. AFAIK, OpenShift is also a containerization solution like docker, Virtuozzo, etc. So, the same solution is valid here. 

Let me describe the situation as I see it: 

A java application runs inside the OpenShift container. This Java application is started with some arguments like that: 

java '-<some options here>,address=5005' SomeApplication

In case of running the application using this command, the application's debugger will accept connections only from the localhost (from the container itself). But in your case the debugger should accept connections from IntelliJ IDEA (this is another host from container's point of view). To pass the information that connections should be accepted from anywhere including IntelliJ IDEA, it is needed to change address=5005 with address=*:5005.

You need to go into the openshift container with your application and edit application's configuration file, so it would start like that: 

java '-<some options here>,address=*:5005' SomeApplication
2
Avatar
Permanently deleted user

I am trying both of them. But error is still continue. 

Firstly, I built project successfully. Then I typed following command to terminal and entered. Project started successfully. I expected to see message like "Listening for transport dt_socket at address 5005". But I didnt see message.

java -jar -Dspring.profiles.active=dev -Dspring-boot.run.jvmArguments="-Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" myproject-0.0.1-SNAPSHOT.jar

After started project then I clicked debug button, unfortunately I had same error.

Also, I watched youtube video at following address and I did the completely same thing;

https://www.youtube.com/watch?v=jA1wHbAK_T0

 

At the end I've two issue; one of this is not see message like "Listening for transport dt_socket at address 5005",

and other is 

 

0

Please change the address in your command as I stated in the previous message: 

Your (address=5005): 

java -jar -Dspring.profiles.active=dev -Dspring-boot.run.jvmArguments="-Xdebug -gentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" myproject-0.0.1-SNAPSHOT.jar

Needed (address=*:5005): 

java -jar -Dspring.profiles.active=dev -Dspring-boot.run.jvmArguments="-Xdebug -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005" myproject-0.0.1-SNAPSHOT.jar
1
Avatar
Permanently deleted user

I've changed. But the error still continues.

0

Can you show the output of the netstat command from inside the container once the application is started? It is needed to check that debugger is listening on all interfaces. In case container is linux-based, that should look like that: 

# netstat -tpln | grep 5005

tcp 0 0 0.0.0.0:5005 0.0.0.0:* LISTEN 29196/java
0
Avatar
Permanently deleted user

I am using windows ;

netstat -aon | findstr "5005"
TCP 127.0.0.1:5005 0.0.0.0:0 LISTENING 8052
0

Am I right that Windows is the OS where IntelliJ IDEA is installed? And your application is running inside OpenShift container (which is most probably running some kind of Linux)? If so, please check the output of netstat inside the OpenShift container. 

0
Avatar
Permanently deleted user

Yes you're right.  But I couldn't use netstat command. When I used I've got "command not found". How can I check the output of netstat inside the OpenShift container?

0

Hi Everyone,

I am experiencing the same problem, below is an image with my configuration:

0
Avatar
Permanently deleted user

Hi Bosa,

I've solved this problem. If you are trying Openshift for remote server, please remove localhost word from your settings as must be following;

 

 

Then add JAVA_DEBUG and JAVA_TOOL_OPTIONS as following to your openshift deployment.

 

0

Unfortunately I am not using OpenShift, but I am using Docker for my remote server 

0
Avatar
Permanently deleted user

Then please find same settings on internet for docker and add.

0

Make sure the port (it is 20446 based on your screenshot) and the JDK version of the remote process that is used to run the remote JVM is the same as set in Remote Debug Configuration. Also make sure the port is not already occupied by another process.

0

I have found the reason but cannot get it solved.

I use other mac as server to test, debug could connect normal. However, when I try to connect to linux server[I have tried both ubuntu 16.6 and centos7.5), connection always reset by the server. So, I cat the TCP protocol with Wireshark, I found that the debugger in IDEA not sending the normal TCP connection, which send PSH before connection was build. Here is the screen shot:

1. TCP successful connected to other mac:

 

2. TCP failed connect to linux server:

 

But I don't know how to solve this, is any one could offer help?

My IDEA version:IntelliJ IDEA 2019.1.2 (Ultimate Edition) Build #IU-191.7141.44, built on May 7, 2019

 

 

update:

use ssh proxy could solve this problem:

ssh -qTfnN -L 0.0.0.0:8094:myserver:8094 root@myserver

2

Verify that you are using same parameters for launching the debug and same JDK version with the linux as with the Mac.

0

I had the same problem, and after doing all the changes that were here exposed (and in other websites), now it work well using the port 5009 instead 5005.

(I am using mac as well)


I lauched my web app through:

gradlew appRunDebug

plugins {
id 'war'
id 'org.gretty' version '3.0.1'
}

// Tell gretty to start and stop the server on test
gretty {
integrationTestTask = 'test'
debugPort = 5009
debugSuspend = false
}

And later I used a Remote Debug inside my IntelliJ IDE:

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

0

I had a similar issue using Docker.  One additional cause for the handshake exception is that your local Java Virtual Machine version is incompatible with the Docker Java Virtual Machine (image) your using.  

eg: It's not possible to handshake between JDK 8 and JDK 11.  However JDK 11 to JDK 8 seems to be ok.   

I found once I made sure the versions were the same, handshaking worked.

0

Hello,


I am using vagrant on ubuntu OS,virtual box, Kubernetes and skaffold, I faced the same issue of : "Unable to open debugger port (localhost:5005): java.io.IOException "handshake failed - connection prematurally closed"

it was working fine, till the windows became 11, after that I faced all the issues, I tried to return it to 10 but with no luck

what I tried is:

1- make JDK (11.0.14) the same at vagrant and local machines.

2- JAVA_TOOL_OPTIONS= 

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

3-

here you can find the configuration of remote debugging.

4- vagrant configuration, and there is another file called 

Vagrant.configure("2") do |config|
config.vm.provider "virtualbox"
config.vm.provision :shell, path: "./forward-port.sh", run: 'always'





config.vm.define :skaffold do |skaffold|
skaffold.vm.box = "bento/ubuntu-18.04"
skaffold.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 443, host: 8443, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 5005, host: 5005, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 5006, host: 5006, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 5007, host: 5007, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 5008, host: 5008, host_ip: "127.0.0.1"
skaffold.vm.network "forwarded_port", guest: 5009, host: 5009, host_ip: "127.0.0.1"

5- forward-port.sh

 

#!/bin/bash
#sudo iptables -t nat -I PREROUTING -p tcp --dport 3306 -j DNAT --to-destination 127.0.0.1:3306
#sudo iptables -t nat -I PREROUTING -p tcp --dport 6379 -j DNAT --to-destination 127.0.0.1:6379

sudo iptables -t nat -I PREROUTING -p tcp --dport 5005 -j DNAT --to-destination 127.0.0.1:5005
sudo iptables -t nat -I PREROUTING -p tcp --dport 5006 -j DNAT --to-destination 127.0.0.1:5006
sudo iptables -t nat -I PREROUTING -p tcp --dport 5007 -j DNAT --to-destination 127.0.0.1:5007
sudo iptables -t nat -I PREROUTING -p tcp --dport 5008 -j DNAT --to-destination 127.0.0.1:5008
sudo iptables -t nat -I PREROUTING -p tcp --dport 5009 -j DNAT --to-destination 127.0.0.1:5009
sudo iptables -t nat -I PREROUTING -p tcp --dport 5010 -j DNAT --to-destination 127.0.0.1:5010

6- 

 run this command jdb -attach 5005

java.io.IOException: handshake failed - connection prematurally closed
        at jdk.jdi/com.sun.tools.jdi.SocketTransportService.handshake(SocketTransportService.java:142)
        at jdk.jdi/com.sun.tools.jdi.SocketTransportService.attach(SocketTransportService.java:255)
        at jdk.jdi/com.sun.tools.jdi.GenericAttachingConnector.attach(GenericAttachingConnector.java:119)
        at jdk.jdi/com.sun.tools.jdi.SocketAttachingConnector.attach(SocketAttachingConnector.java:83)
        at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.attachTarget(VMConnection.java:519)
        at jdk.jdi/com.sun.tools.example.debug.tty.VMConnection.open(VMConnection.java:328)
        at jdk.jdi/com.sun.tools.example.debug.tty.Env.init(Env.java:63)
        at jdk.jdi/com.sun.tools.example.debug.tty.TTY.main(TTY.java:1095)

Fatal error:
Unable to attach to target VM.
0

Saraham seems like the remote JVM process is not accessible via the port. Double check your network configuration, firewall (inlcuding Windows firewall/antivirus) - that they are not blocking ports and that remote interface is routable.

0

Please sign in to leave a comment.