I’m running my Java application with the following parameters that enable JMX server’s remote connections:
-Dcom.sun.management.jmxremote=true
-Dcom.sun.management.jmxremote.port=9998
-Dcom.sun.management.jmxremote.authenticate=false
-Dcom.sun.management.jmxremote.ssl=false
I can successfully connect to that JMX server via VisualVM by adding a new JMX Connection to address: <HOSTNAME>:9998
But when i run that application inside Windows Subsystem for Linux 2 (WSL2) Visual VM cannot connect to localhost:9998
:
connection failed
For what I know, every port that the application is listening on in WSL2 is also opened in host machine (Windows itself), and I can check that using Win+R -> cmd -> telnet localhost 9998
(connected successfully).
Also, I’m running an nginx instance inside WSL2 that I can connect to by any browser on my host machine (Windows) using localhost
.
4
Answers
This seems to be a general issue with Java.
https://github.com/microsoft/WSL/discussions/6253
A workaround that worked for me was listening on ‘0.0.0.0’ instead of ‘127.0.0.1’ in WSL2 with the JMX server.
I wasn’t able to get Windows jconsole to connect to a JVM running in WSL2 using any of the suggestions above, however it is now possible to run Linux jconsole in WSL2, using Windows 11’s Linux GUI support. Works perfectly.
I had the same problem and finally I found a working set of properties for my WSL2. Additionally I had additional requirement to set it up for docker container running inside WSL2, while JConsole/VisualVM was running on Windows host. I have also a VPN running which probably mess up networking even more.
I found this application https://github.com/cluther/jmx-tester to be useful in debugging the connection.
inside WSL2
Then on Windows host connect JMX to
127.0.0.1:9991
docker container inside WSL2
Then on Windows host connect JMX to
127.0.0.1:9991
In my case it depends on
java.net.preferIPv6Addresses
https://docs.oracle.com/en/java/javase/17/docs/api/java.base/java/net/doc-files/net-properties.htmlDefault value is false
I have server running on WSL2 and client running on Win11: