Running code on program exit in Java [duplicate] August 21, 2023 by Tarik Use Runtime.getRuntime().addShutdownHook(Thread).