开发者

Open and close applications from Java

I want to make a program to open and close applications you have installed on your computer.

I know that using

Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+"M:\\myfile.开发者_如何学JAVAdoc"); 

open the Word document myfile.doc

The problem is using this command can not close the Word application that opens when you execute the command.

How I can open and close applications from a java program?

Thank's!

Regards!


You could try by storing the Processreturned by exec() and calling destroy() on it but I don't think it will work since you are executing a windows command that launches a new process which is then not managed by the JVM (only the rundll32 thing is seen by JVM).

A solution that will work for sure but won't be cross-platform (I don't think you actually care since Word is not cross-platform too :) is to use another windows shell command to obtain what you want, like

taskkill /IM word.exe

You could execute it in the same way like you do to open word, with defaultRuntime.exec(...). If you have many word processes opened and you want to kill a specific one you will have to parse the task list to obtain the correct PID and then try with taskkill /PID XXXX.


I'm not the Java expert, however recently I had contact with the function mentioned by You. As I recall, Runtime.getRuntime().exec() return a Process class object.

You can try doing as follows:

Process word = Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL "+"M:\\myfile.doc");
// some logic
word.destroy();

Maybe it will solve Your closing problem.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜