Run the .bat file when closing java application.

I want to run the file .bat

when I close my Java application.

I try, but when I close my Java application, the bat file won't start.

+2


a source to share


4 answers


You might want to look at the stop hooks. Basically, you can register a non-stationary thread, it will be started by the JVM when the process starts.

Runtime.getRuntime().addShutdownHook(thread)

      

According to the doc:



The Java Virtual Machine shuts down in response to two kinds of events:

  • The program exits normally when the last non-daemon thread exits or when the exit method is called (equivalent to System.exit), or
  • The virtual machine shuts down in response to a user interrupt, such as typing ^ C or a system-wide event such as a user shutdown or system shutdown.
+4


a source


I can imagine two options.

  • Start your Java application from a single batch or ant file and then automatically start it when done.
  • Intercept when your application is closed with java code, then run the batch file launched from Java as a subprocess. You will likely have to use Windows special methods to make them work after exiting if you don't want your application to wait.


Only 1 will work if your process is killed. How are you trying to do this at the moment, and how does it happen? Without this information, we cannot help you.

+1


a source


Please go through this http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ProcessBuilder.html . Use Runtime.getRuntime (). AddShutdownHook (stream) to bind it.

0


a source


If your app is a desktop app extending SingleFrameApplication

, you can override its exit method: public void exit(EventObject event)

and add code to run your file.

0


a source







All Articles