Arphadax
Arphadax

Reputation: 63

how to get java to run an application and when user closes the application, java runs another process in mac or windows

Hi all i m trying to write a java program to open an application on a mac, wait in the background until the user closes the application, then the java program performs another task. is there a way to know when the user has closed the application started by java?

thanks

Upvotes: 1

Views: 451

Answers (1)

aioobe
aioobe

Reputation: 421040

is there a way to know when the user has closed the application started by java?

Sure, here's how:

  1. Use ProcessBuilder or Runtime.exec to get hold of a Process.
  2. Start the process by doing process.start().
  3. Then call proccess.waitFor() to block until the external program terminates.

This should work fine on Mac and Windows systems.


Example:

ProcessBuilder pb = new ProcessBuilder("/usr/bin/emacs");

Process proc = pb.start();     // start external program

proc.waitFor();                // wait for it to terminate

performAnotherTask();

Upvotes: 3

Related Questions