views:

32

answers:

2

Is there any way to start non-Java process from Java and then stop it? Or at least send some keyinput to it (e.g. alt+f4)?

E.g. I start java app, then javaapp start notepad, then javaapp send alt+f4 to notepad. Javaapp will run from Administrator account.

Question is only about Windows OS.

A: 

You can start a new process with Runtime.exec(). Then get the standard input of the returned process with Process.getOutputStream() and push input to it. In the end you can destroy() it.

Note that you must consume all output from the process to make it run correctly.

Péter Török
A: 

EDIT: missed question about starting...

To create, use Runtime.getRuntime().exec()

To destroy, use: Process.destroy() From the javadoc:

Kills the subprocess. The subprocess represented by this Process object is forcibly terminated.

Tim Bender
thx for answer, it was exactly that I need
kusoksna
No problem. I couldn't find an answer to your comment/addendum.
Tim Bender