I am working on Java RMI. I am having little issue with running my rmiregistry on port 2028 as I already used that one to run my test program. I can run my program using other port but I would like to know, How we can close rmiregistry running on particular port ?
-
Are you starting rmiregistry programatically or manually as a separate process? – Tudor Dec 05 '11 at 13:26
-
No. I am starting it Linux shell like following % rmiregistry 2028& – slonkar Dec 05 '11 at 13:34
-
If you are starting it manually, can't you also stop it manually? – Tudor Dec 05 '11 at 13:34
-
I could not find command to do so. That's why put this question up here... – slonkar Dec 05 '11 at 13:38
4 Answers
If you want to do this in programming, we do something like:
// create the registry
Registry rmiRegistry = LocateRegistry.createRegistry(port);
...
// connect to it
JMXConnectorServer connector =
JMXConnectorServerFactory.newJMXConnectorServer(url,
new HashMap<String, Object>(),
ManagementFactory.getPlatformMBeanServer());
// do stuff with it ...
// close the connection
if (connector != null) {
connector.stop();
}
// deregister the registry
if (rmiRegistry != null) {
UnicastRemoteObject.unexportObject(rmiRegistry, true);
}
Here's the full code for our JMXServer class. We have problems creating 2 of these and completely unregistering them so we make sure to run our unit tests on different ports.
I use this code in my SimpleJmx JMX client/service package.

- 115,027
- 24
- 293
- 354
-
-
1I was having a similar problem in Eclipse, where the registry thread would just continue running forever with the code snippets above. Added System.exit(0) to the end, which kills all threads in the JVM, and all ends well ;-) – Chuck Mosher Dec 27 '12 at 21:15
-
+1 I was stopping the connector but couldn't work out how to get rid of the registry's server socket. Thanks for the answer. – dannrob Aug 23 '16 at 06:12
After so much of hassle I suddenly realize that rmiregistry runs in background of shell. So all we have to do close it first bring it to foreground and then close it. And it worked.
BTW to bring it to foreground just type:
% fg
and then to close it type:
Ctrl + c
That's it. Thanks a lot everyone who tried to help me out.

- 8,356
- 19
- 50
- 61

- 4,055
- 8
- 39
- 63
If the rmi registry is already using the port and you want to rebind a service without using another port. You can try the code below
Registry registry = null;
try {
registry = LocateRegistry.createRegistry(1099);
} catch (ExportException ex) {
registry = LocateRegistry.getRegistry(1099);
} catch (RemoteException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}

- 1,150
- 4
- 13
- 28
If you are running rmiregistry from the shell try to close it with:
Process p =
Runtime.getRuntime().exec("ps -ef | grep rmiregistry | awk '{ print $2 }' | kill -9");
I'm not so fresh with shell commands, but I hope you get the idea.

- 61,523
- 12
- 102
- 142
-
I guess from shell I will have to use ps -ef | grep rmiregistry. I have not tried it yet. But I will sure let you know. Thanks. – slonkar Dec 05 '11 at 17:18
-
@ Tudor: Sorry man it did not worked out... But I did found the answer to do so. – slonkar Dec 08 '11 at 11:21