Java 检查某些 exe 程序是否在 Windows 上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19005410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 13:18:26  来源:igfitidea点击:

check if some exe program is running on the windows

javaprocessexe

提问by duka.milan

How to check if some .exe program is running (is in process) on Windows?

如何检查某些 .exe 程序是否在 Windows 上运行(正在处理)?

I'm making java application which update one .exe program. So, if that exe program is used by some client, my application ask for closing exe program, and after closing automatically replace .exe file with new one.

我正在制作更新一个 .exe 程序的 Java 应用程序。因此,如果某个客户端使用该 exe 程序,我的应用程序会要求关闭 exe 程序,并在关闭后自动将 .exe 文件替换为新文件。

采纳答案by Aneesh

You can run the following statement in your java program. Before that you need to know the name of the task in task manager. Say you want to see MS-Word is running. Then run MS-Word, go to task manager and under the process tab, you should see a process named word.exe. Figure out the name for the process you are targeting. Once you have that, you just run the following code:

您可以在 Java 程序中运行以下语句。在此之前,您需要知道task manager. 假设您想查看 MS-Word 正在运行。然后运行 ​​MS-Word,转到任务管理器,在进程选项卡下,您应该看到一个名为word.exe. 找出您所针对的流程的名称。一旦你有了它,你只需运行以下代码:

String line;
String pidInfo ="";

Process p =Runtime.getRuntime().exec(System.getenv("windir") +"\system32\"+"tasklist.exe");

BufferedReader input =  new BufferedReader(new InputStreamReader(p.getInputStream()));

while ((line = input.readLine()) != null) {
    pidInfo+=line; 
}

input.close();

if(pidInfo.contains("your process name"))
{
    // do what you want
}

回答by TheLostMind

You can try running the following code :

您可以尝试运行以下代码:

Runtime rt = Runtime.getRuntime();

and execute "tasklist"

并执行“任务列表”

tasklist returns a list of currently executing processes (as shown in the task manager's process tab).

tasklist 返回当前正在执行的进程列表(如任务管理器的进程选项卡中所示)。

回答by BullyWiiPlaza

Here's a full code for checking if an application is running on a Windowssystem or not:

这是用于检查应用程序是否在Windows系统上运行的完整代码:

import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Scanner;

public class ApplicationUtilities
{
    public static void runApplication(String applicationFilePath) throws IOException, InterruptedException
    {
        File application = new File(applicationFilePath);
        String applicationName = application.getName();

        if (!isProcessRunning(applicationName))
        {
            Desktop.getDesktop().open(application);
        }
    }

    // http://stackoverflow.com/a/19005828/3764804
    private static boolean isProcessRunning(String processName) throws IOException, InterruptedException
    {
        ProcessBuilder processBuilder = new ProcessBuilder("tasklist.exe");
        Process process = processBuilder.start();
        String tasksList = toString(process.getInputStream());

        return tasksList.contains(processName);
    }

    // http://stackoverflow.com/a/5445161/3764804
    private static String toString(InputStream inputStream)
    {
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\A");
        String string = scanner.hasNext() ? scanner.next() : "";
        scanner.close();

        return string;
    }
}

For instance, you could use the runApplication()method to only run the application when it is not running yet:

例如,您可以使用该runApplication()方法仅在尚未运行时运行应用程序:

ApplicationUtilities.runApplication("C:\Program Files (x86)\WinSCP\WinSCP.exe");

The same principle applies for deleting the executable.

同样的原则适用于删除可执行文件。

回答by Mr. Michael Eder

Just a suggestion for users of Java 9or higher.
It's even operating system independent:

只是对 Java 9或更高版本用户的建议。
它甚至独立于操作系统:

Interface ProcessHandle
static Stream<ProcessHandle>    allProcesses?()

More details at:
https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html

更多详情请访问:
https://docs.oracle.com/javase/9/docs/api/java/lang/ProcessHandle.html