If you want to start jstatd on a specific port for its RMI registry, you can pass -p parameter for this.
But the problem is that it opens a second random port (called "anonymous port" in java) which causes problem for writing firewall rules or to use JVisualVM to connect to a remote jstatd running in a Docker container.
If you look at jstatd source, you'll see that it is calling UnicastRemoteObject.exportObject(remoteHost, 0) which will open a new "anonymous port" which seems to be random.
Is there a way to force this last port to a fixed one, or a way to predict which one will be chosen?
I found no easy way to predict which concrete port will be opened by using an anonymous port.
But I found a rewrite of
jstatdcalled "jakestatd" which will force the 3 ports (because at last, I discovered thatjstatdactually opens 3 ports and not 2 as I first thought) thatjstatduses.As it was not enough for me because I needed to control those ports, I wrote ejstatd that answer this exact question (as well as others), so now I can control thos ports using (inside ejstatd's folder):
Here the 3 ports that will be opened will be
2222,2223and2224, and the RMI registry will be available at port2222.