Summary of java program opening and closing exe program problems

It took two days to study the problem of java program opening an external exe program and closing the already opened exe process. The summary is as follows:

Scenario: Develop a visual window, two buttons, start, close. Implement the MouseListener interface for the two buttons to perform actions when the mouse button is pressed. Here's what the two buttons do.

start up:

Runtime.getRuntime().exec("E:/myClient/punchClient.exe");//How to start the .exe file

Runtime.getRuntime().exec("cmd.exe /c start c://example.exe"); is also one of the methods.

Since this function just starts a project and has no other operations, Process is not declared to receive the return value. It seems that it is very simple to open an exe file. As for the usage of Runtime and the usage of the exec method, you can download it from Baidu, and I won't say more.

closure:

For how to close an exe process in a java program, search Baidu and summarize it as follows:

taskkill--window under linux, the user who executes this operation with kill must have the kill command permission

For taskkill, if executing the command prompt in the cmd window is not an internal command, but double-clicking in windows\system32 can be used, then add the full path and try it.

String command = "cmd.exe /c c:\\windows\\system32\\taskkill /f /im  punchClient.exe";

Process proc =Runtime.getRuntime().exec(command);

The meaning of the parameters of taskkill is not introduced here.

This way fails to run in my java environment. Next try this:

 Runtime.getRuntime().exec("tskill punchClient"); the test is successful

Note that here is the tskill process name without .exe, it will not work with it.

tskill PID/ProcessName

In this case, look up the process list, take its PID, and try

Runtime.getRuntime().exec("tskill 5036"); also succeeds,

 

There is another way:

Runtime.getRuntime().exec("cmd.exe /cc:\\windows\\system32\\taskkill /f /pid 5036"); the test is successful

Because of my path problem, here must be the full path.

 

In addition: The key code of the method to obtain the PID is as follows

 

Process listprocess = Runtime.getRuntime().exec("cmd.exe /c tasklist");

InputStream is = listprocess.getInputStream();

byte[] buf = new byte[256];

BufferedReader r = new BufferedReader(new InputStreamReader(is));

StringBuffer sb = new StringBuffer();

String str = null;

while ((str = r.readLine()) != null) {

 String id = null; 

Matcher matcher = Pattern.compile(programName + "[ ]*([0-9]*)").matcher(str); 

while (matcher.find()) {  

if (matcher.groupCount() >= 1) {   

id = matcher.group(1);   

if (id != null) {   

 Integer pid = null;    

try {     

pid = Integer.parseInt(id);    

} catch (NumberFormatException e) {    

 e.printStackTrace ();   

 }   

 if (pid != null) {     

Runtime.getRuntime().exec("cmd.exe /c taskkill /f /pid " + pid);     

System.out.println("kill progress");   

 }  

 }  

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326569981&siteId=291194637