Google+ Badge

Friday, August 22, 2008

How to exit a Java App cleanly!

Exit Java Application

Have you ever started a java app from an SSH session and when you are done you try to exit the SSH session and it's not exiting. Another symptom would be to call the java app from a script and the script never regains contol from the Java App. The solution is simple...

Your program still has descriptors for stdin, stdout and stderr open and sshd is waiting for them to close. Either have your program close those descriptors or redirect them to /dev/null. You probably also want to use "nohup" to prevent the program from exiting on SIGHUP. Here is an example that should help you....

nohup java -cp . Test /dev/null 2>&1 &