I am not sure you can. kill -9 nameofapp doesn't communicate with the app, it tells the OS to cut the apps throat.
You could write a script to communicate with your app to close files or whatever, and then call kill. I doubt it would be worthwhile to do so especially if your program is used by more then just you and on different platforms, and it still doesn't stop someone from directly invoking kill.
I think the best thing to do is to make sure that the end user won't need to use kill to shut down your program.
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
You can use the Runtime.getRuntime().addShutdownHook(Thread) method to specify a Thread that will be started when the application is terminated for any reason. Read its API documentation for all the gory details.
I am aware that the replies posted above are More Experienced and more technical than one would expect. So i would only like to add in that the above mentioned commands cause abnormal JVM shutdown so even a finally block cannot be helpful at all.