What port is used by Java RMI connection?
RMI generally won't work over a firewall, since it uses unpredictable ports (it starts off on 1099, and then runs off with a random port after that).
In these situations, you generally need to resort to tunnelling RMI over HTTP, which is described well here.
All the answers so far are incorrect. The Registry normally uses port 1099, but you can change it. But that's not the end of the story. Remote objects also use ports, and not necessarily 1099.
If you don't specify a port when exporting, RMI uses a random port. The solution is therefore to specify a port number when exporting. And this is a port that needs opening in the firewall, if any.
-
In the case where your remote object extends
UnicastRemoteObject
, have its constructor callsuper(port)
with some non-zero port number. -
In the case where it doesn't extend
UnicastRemoteObject
, provide a non-zero port number toUnicastRemoteObject.exportObject()
.
There are several wrinkles to this.
-
If you are using socket factories, your
RMIServerSocketFactory
must have a sensible implementation ofequals()
for port sharing to work, i.e. one that doesn't just rely on object identity via==
orObject.equals()
. -
If either you don't provide a server socket factory, or you do provide one with a sensible
equals()
method, but not both, you can use the same non-zero explicit port number for all remote objects, e.g.createRegistry(1099)
followed by any number ofsuper(1099)
orexportObject(..., 1099)
calls.