The finalize method
I'm developing a rmi server that makes some jni calls to C++ dll's. The problem is that when I shut the server down, (currently done by Ctrl+C) I want the server to call a clean up function in the dll.
I thought the way to do it would be to have a finalize method call this function for me, but I can't seem to get it to work. So I concluded that eather finalize doesn't like calling JNI function or more likely that the object is still refrenced.
Could this be the RMI that is keeping a refrence? and if so how do I tell java to release it?
Thanks
[584 byte] By [
stonest] at [2007-9-26 8:02:19]

Control-c sounds a bit brutal to me. I'm not quite sure what will really happen between RMI and finalize in that case.
How about instead coding up a start/stop as part of your command input?
Have your main() method do the following:
o On a "start" command, try to RMI connect to the server. If successful, then error out - user is attempting to start a second copy. Otherwise, complete the startup by registering the server with the registry.
o On a "stop" command, try to connect to the server. If successful, tell it via RMI to stop. (The server can then unregister with RMI and do the C cleanup.) Otherwise, report that the server isn't running.
>...have a finalize method call this function for me...
There is nothing in any specification that says that finalize must be called at all. Exiting an application is the most likely place it will not be called.
You might want to look at Runtime.addShutdownHook(Thread hook). I believe the methods in it are called when control-C is pressed (although you might want to test what happens if control-C is pressed twice.)