An attempt to create more than a single registry in the same VM is failing.

Working as designed. You can't create more than one Registry per JVM. This is because of what it says in the Javadoc: the Registry instance is exported with a well-known object identifier, an ObjID instance constructed with the value ObjID.REGISTRY_ID.'

How to stop the existing running RMI registry and re-start the RMI registry with same port number ? Can i do registry.unbind("xx") and registry.bind("xxx") again for starting the RMI registry on the same port number ?

So this was the 2nd Registry, so it failed. Of course. Trying to create two Registries in the same JVM isn't going to work, and trying to create a 2nd Registry and then immediately unexport it doesn't make any sense whatsoever. I don't understand any of this. I don't know why you think you need a 2nd Registry at all. You don't.

No you aren't. There is nothing in that code that will cause an ExportException. It won't work, for another reason, because you didn't read what I wrote when I told you about unexportObject(). I didn't say a word about getRegistry(). And if it did work, which it can't, it would be 100% redundant to unbind before stopping the Registry.

It is time for you to stop guessing and explain what this is all about. You just seem to be flailing about aimlessly frankly.

It is in fact possible to create more than one registry in a JVM; just call LocateRegistry.createRegistry(port) with different port numbers. But EJP is right: it is very, very unusual ever to want to create more than one registry within a JVM.

There is an important clue in your output listing, where each line is prefixed with "[testng]". You're trying to run a bunch of tests that use RMI under TEST-NG, right? If so (and I'm guessing a bit here) then what you should probably do is start up a single registry using a call to LocateRegistry.createRegistry() in your test suite's @BeforeSuite method. Save this reference somewhere. Then, in the @AfterSuite method, unexport the reference to the registry created in @BeforeSuite. Or, if the JVM is going to exit after running the suite, you probably don't need to unexport the registry, since it'll die with the JVM.

Before each test, in the @BeforeMethod method, create the server object, export it, and bind it into the registry. In the @AfterMethod method, unbind it, unexport it, and (if necessary) destroy any state associated with the server object. Again, I'm guessing, but you probably want to create a fresh server object for each test, so that actions of earlier tests don't affect the results of subsequent tests.

EJP wrote:
Fancy that. Completely forgot what I wrote five years ago. How unusual ;-)

Of course, that never happens to me. :-)

Thinking further about this issue, it might be that the OP ("890918") doesn't need to start up a registry at all. If all of the tests are indeed running within the same JVM, they can call the stub directly instead of passing it through a registry. Of course, this is all a mystery until the OP tells us what is really going on.

Yes, trying to run a bunch of tests that use RMI under TEST-NG. Still getting Caused by: java.rmi.server.ExportException: internal error: ObjID already in use. I tried UnicastRemoteObject.unexportObject(Registry, true) before binding. Seems still port is not released. It is not default rmi registry port number.

can call the stub directly instead of passing it through a registry.
How stub is called directly instead of passing it through a registry ?