Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to end a process nicely in a Java application?

Tags:

java

In a Java application:

currentProcess = Runtime.getRuntime().exec("MyWindowsApp.exe");
...
currentProcess.destroy();

Calling destroy simply kills the process and doesn't allow any user cleanup or exit code to run. Is it possible to send a process a WM_CLOSE message or similar?

like image 579
Chris Haines Avatar asked Jun 05 '09 10:06

Chris Haines


2 Answers

You could use Process.getOutputStream to send a message to the stdin of your app, eg:

PrintStream ps = new PrintStream(currentProcess.getOutputStream());
ps.println("please_shutdown");
ps.close();

Of course this means you have to contrive to listen on stdin in the Windows app.

like image 127
Zarkonnen Avatar answered Sep 25 '22 01:09

Zarkonnen


you can try with JNA, importing user32.dll and defining an interface that defines at least CloseWindow

like image 28
dfa Avatar answered Sep 25 '22 01:09

dfa