문제

요 Java 방법을 찾아 실행하는 승리 프로세스에서는 내가 아는 이름의 성능을 크게 향상시킵니다.보고 싶은 실행 중인지 여부를 지금 내가 필요로 하는 방법 프로세스를 종료하는 경우 내가 그것을 발견했습니다.

도움이 되었습니까?

해결책

당신이 사용할 수 있는 명령 윈도우 도구 tasklisttaskkill 와 통화에서 그들을 사용하여 Java Runtime.exec().

다른 팁

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

 }

예제:

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

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

 if (isProcessRunning(processName)) {

  killProcess(processName);
 }
}

가는 API 를 제공하여 원하는 기능:

https://github.com/kohsuke/winp

윈도우 프로세스 라이브러리

당신이 사용할 수 있는 명령행 도구를 죽이는 프로세스 SysInternals PsKillSysInternals 사실 Pslist.

당신은 수도에서 빌드를 사용하는 tasklist.exe 고 taskkill.exe 지만,그들에서만 사용할 수 있는 윈도우 XP Professional 이상(지 않는 가정에서 버전)입니다.

java.랭.런타임입니다.exec 을 실행하는 프로그램입니다.

여기에서의 그루비한 방법을:

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}")
}

를 사용하여 다음과 같은 클래스 죽이는 윈도우 프로세스 (실행 중인 경우).나의 힘을 사용하여 명령 라인에 인수 /F 는지 확인하는 프로세스에 의해 지정 /IM 인수를 종료됩니다.

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

당신이 전화를 몇 가지 기본 코드,이럴 때문에 없는 라이브러리는 않습니다.이후 JNI 은 복잡하고 열심히 사용 하려고 할 수 있습니다 JNA(Java 기본 액세스). https://jna.dev.java.net/

작은 변화에 대답을 작성해 슈퍼 kakes

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

변경되었습니다.

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

/IMF 옵션하지 않습니다.그것은 죽이지 않는 메모니다. /IM 옵션 실제로 작동하는

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top