简体   繁体   中英

Java Runtime, Perform some action on closing a application

We can open Notepad or Calculator or any application using Java Runtime say for example To open calculator we need to run below line of code in java.

Runtime.getRuntime().exec("calc");

but I want to do some action on closing the app that I opened through above line of code.

  1. Is there anyway to perform something on onClosing application?
  2. How to check whether application is opened or not?

You can do something like this:

Process proc = Runtime.getRuntime().exec("calc");
proc.waitFor();

waitFor will wait for the termination of the application. If you don't want to wait, you can call proc.isAlive() to test the process status. You can also start a java thread that will start the application and wait for it's termination.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM