2008-09-17 11 views

Odpowiedz

16

Możesz użyć narzędzi Windows na linii poleceń tasklist i taskkill i wywoływać je z poziomu Javy przy użyciu Runtime.exec().

0

Będziesz musiał zadzwonić do natywnego kodu, ponieważ IMHO nie ma biblioteki, która to robi. Ponieważ JNI jest kłopotliwy i trudny, możesz spróbować użyć JNA (Java Native Access). https://jna.dev.java.net/

2

Można użyć narzędzia wiersza poleceń do zabijania procesów, takich jak SysInternals PsKill i SysInternals PsList.

Można również użyć wbudowanych tasklist.exe i taskkill.exe, ale są one dostępne tylko w systemie Windows XP Professional i nowszych wersjach (nie w wersji Home Edition).

Użyj java.lang.Runtime.exec, aby uruchomić program.

3

Jest trochę API zapewniając pożądaną funkcjonalność:

https://github.com/kohsuke/winp

Biblioteka procesów systemu Windows

+0

Czy istnieje dobry URL tego interfejsu API? – dumbledad

+1

naprawiono link. – andreas

+0

Naprawiono link ponownie –

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); 

} 

PRZYKŁAD:

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

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

if (isProcessRunning(processName)) { 

    killProcess(processName); 
} 
} 
+3

Jeśli taskkill nie zakończy twojego procesu, możesz dodać parametr "/ F", aby go wymusić. –

+0

wygląda dobrze! Nie wiedziałem, że to jest ważna opcja tutaj. Zaktualizowałem odpowiedź. Thanx –

+0

minus 1, ponieważ polecenie taskkill ma niepoprawną składnię. Powinien to być "" taskkill/F/IM "' – BullyWiiPlaza

1

Oto Groovy sposób to zrobić:

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

mała zmiana w odpowiedzi pisemnej przez Super Kakes

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

Zmieniono ..

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

/IMF opcja doesnot pracy .To nie zabija notepad..while /IM opcja faktycznie działa

1

Użyj następującej klasy do kill a Windows process (if it is running). Używam argumentu force command line /F, aby upewnić się, że proces określony w argumencie /IM zostanie zakończony.

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(); 
    } 
} 
Powiązane problemy