Anthony O. Anthony O. - 3 months ago 31
Java Question

How to force or predict the second open port of jstatd?

If you want to start

on a specific port for its RMI registry, you can pass
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
running in a Docker container.

If you look at
, 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 jstatd called "jakestatd" which will force the 3 ports (because at last, I discovered that jstatd actually opens 3 ports and not 2 as I first thought) that jstatd uses.

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):

mvn exec:java -Dexec.args="-pr 2222 -ph 2223 -pv 2224"

Here the 3 ports that will be opened will be 2222, 2223 and 2224, and the RMI registry will be available at port 2222.