I am trying to get a shutdown hook to run in java. The purpose of this hook is to write into a log file that the program was terminated. The java application is compiled into a jar file and executed using a batch file, which means that the output of the application goes to a cmd.exe window. When this cmd window is closed, i want the application to log that it was closed.
The application is intended to run indefinitely and do certain tasks at a certain time. Therefore, the program is executing a series of checks within a "While true" loop. I do not understand why this hook will not log that the application is closing!
In the main method we have:
final Thread mainThread = Thread.currentThread();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable()
{
@Override
public void run()
{
try { mainThread.join(); }
catch (InterruptedException e) { }
mission.log("Program Closed.", true);
}
}
));
All "mission" is, is an instance of a custom class of mine that does data logging. When you call the method "log", it has a BufferedWriter and it calls its .write(), .newLine(), and .flush() methods.
I can't seem to get the "Program Closed" line in my logfile when i kill the batch window. What am I doing wrong?