2008-09-17 12 views

Trả lời

16

Bạn có thể sử dụng các công cụ cửa sổ dòng lệnh tasklisttaskkill và gọi chúng từ Java bằng cách sử dụng Runtime.exec().

0

Bạn sẽ phải gọi một số mã gốc, vì IMHO không có thư viện nào thực hiện. Kể từ khi JNI là cồng kềnh và khó khăn, bạn có thể thử sử dụng JNA (Java Native Access). https://jna.dev.java.net/

2

Bạn có thể sử dụng công cụ dòng lệnh để giết các quy trình như SysInternals PsKillSysInternals PsList.

Bạn cũng có thể sử dụng tasklist.exe và taskkill.exe tích hợp sẵn, nhưng chỉ có sẵn trên Windows XP Professional và phiên bản mới hơn (không có trong Home Edition).

Sử dụng java.lang.Runtime.exec để thực thi chương trình.

3

Có một API ít cung cấp các chức năng mong muốn:

https://github.com/kohsuke/winp

của Windows Process Library

+0

Có URL tốt cho API này không? – dumbledad

+1

đã sửa liên kết. – andreas

+0

cố định liên kết lại –

34
private static final String TASKLIST = "tasklist"; 
private static final String KILL = "taskkill /F /IM "; 

public static boolean isProcessRunning(String serviceName) throws Exception { 

Process p = Runtime.getRuntime().exec(TASKLIST); 
BufferedReader reader = new BufferedReader(new InputStreamReader(
    p.getInputStream())); 
String line; 
while ((line = reader.readLine()) != null) { 

    System.out.println(line); 
    if (line.contains(serviceName)) { 
    return true; 
    } 
} 

return false; 

} 

public static void killProcess(String serviceName) throws Exception { 

    Runtime.getRuntime().exec(KILL + serviceName); 

} 

VÍ DỤ:

public static void main(String args[]) throws Exception { 
String processName = "WINWORD.EXE"; 

//System.out.print(isProcessRunning(processName)); 

if (isProcessRunning(processName)) { 

    killProcess(processName); 
} 
} 
+3

Nếu taskkill không kết thúc quá trình của bạn, bạn có thể muốn thêm tham số "/ F" để ép buộc. –

+0

ngoại hình đẹp! Không biết đó là một lựa chọn hợp lệ ở đây. Tôi đã cập nhật câu trả lời. Thanx –

+0

trừ 1 vì lệnh taskkill có cú pháp không hợp lệ. Nó phải là '" taskkill/F/IM "' – BullyWiiPlaza

1

Đây là một cách hấp dẫn để làm việc đó:

final Process jpsProcess = "cmd /c jps".execute() 
final BufferedReader reader = new BufferedReader(new InputStreamReader(jpsProcess.getInputStream())); 
def jarFileName = "FileName.jar" 
def processId = null 
reader.eachLine { 
    if (it.contains(jarFileName)) { 
     def args = it.split(" ") 
     if (processId != null) { 
      throw new IllegalStateException("Multiple processes found executing ${jarFileName} ids: ${processId} and ${args[0]}") 
     } else { 
      processId = args[0] 
     } 
    } 
} 
if (processId != null) { 
    def killCommand = "cmd /c TASKKILL /F /PID ${processId}" 
    def killProcess = killCommand.execute() 
    def stdout = new StringBuilder() 
    def stderr = new StringBuilder() 
    killProcess.consumeProcessOutput(stdout, stderr) 
    println(killCommand) 
    def errorOutput = stderr.toString() 
    if (!errorOutput.empty) { 
     println(errorOutput) 
    } 
    def stdOutput = stdout.toString() 
    if (!stdOutput.empty) { 
     println(stdOutput) 
    } 
    killProcess.waitFor() 
} else { 
    System.err.println("Could not find process for jar ${jarFileName}") 
} 
0

thay đổi nhỏ trong câu trả lời được viết bởi Siêu kakes

private static final String KILL = "taskkill /IMF "; 

Thay đổi để ..

private static final String KILL = "taskkill /IM "; 

/IMF tùy chọn doesnot việc .it không giết notepad..while /IM tùy chọn thực sự hoạt động

1

Sử dụng lớp sau đây để kill a Windows process (if it is running). Tôi đang sử dụng đối số dòng lệnh buộc /F để đảm bảo rằng quá trình được chỉ định bởi đối số /IM sẽ bị chấm dứt.

import java.io.BufferedReader; 
import java.io.InputStreamReader; 

public class WindowsProcess 
{ 
    private String processName; 

    public WindowsProcess(String processName) 
    { 
     this.processName = processName; 
    } 

    public void kill() throws Exception 
    { 
     if (isRunning()) 
     { 
      getRuntime().exec("taskkill /F /IM " + processName); 
     } 
    } 

    private boolean isRunning() throws Exception 
    { 
     Process listTasksProcess = getRuntime().exec("tasklist"); 
     BufferedReader tasksListReader = new BufferedReader(
       new InputStreamReader(listTasksProcess.getInputStream())); 

     String tasksLine; 

     while ((tasksLine = tasksListReader.readLine()) != null) 
     { 
      if (tasksLine.contains(processName)) 
      { 
       return true; 
      } 
     } 

     return false; 
    } 

    private Runtime getRuntime() 
    { 
     return Runtime.getRuntime(); 
    } 
}