Java - 在 Windows 7 中通过命令行获取外部进程的 PID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11685408/
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-10-31 06:02:50  来源:igfitidea点击:

Java - get PID of external process by command line in Windows 7

javacommand-linepid

提问by annih

I have Windows 7 32 bit with Java:

我有带 Java 的 Windows 7 32 位:

How do I get the PID of a process by command line in Windows 7?

如何在 Windows 7 中通过命令行获取进程的 PID?

I want to kill an application which I only can truly identify by the command line which ran it. We have several Java applications running on that machine. I need to stop specific ones.

我想杀死一个我只能通过运行它的命令行来真正识别的应用程序。我们在那台机器上运行了几个 Java 应用程序。我需要停止特定的。

To be exact: I need to find tomcat which is run by catalina.bat. What do you think is the best way to do this?

确切地说:我需要找到由catalina.bat. 你认为最好的方法是什么?

I know of tasklist, but it does not seem to be able to query the command line which started the process. Finding java.exedoes not help me. I tried searching for something useful like pgrep/pkill for Windows, with no success.

我知道 tasklist,但它似乎无法查询启动该进程的命令行。寻找java.exe对我没有帮助。我尝试搜索一些有用的东西,比如 Windows 的 pgrep/pkill,但没有成功。

采纳答案by annih

Finally found something. The solution working for me is called wmic (Windows Management Instrumentation Commandline). This nice tool comes built-in to Windows 7 Pro (mine) and possibly other Windows versions. It provides a quite large variety of actions like listing all the running tasks with every details you can imagine (like their corresponding command line), various hardware info, etc. Exactly what I need.

终于找到东西了。对我有用的解决方案称为 wmic(Windows Management Instrumentation Commandline)。这个不错的工具内置于 Windows 7 Pro(我的)和可能的其他 Windows 版本中。它提供了种类繁多的操作,例如列出所有正在运行的任务以及您可以想象的每个细节(例如它们对应的命令行)、各种硬件信息等。正是我需要的。

回答by Konstantin V. Salikhov

You could use jps -lvcommand to determine java process by it's command line options. jps is utility that included in many up-to-date JDKs.

您可以使用jps -lvcommand 通过它的命令行选项来确定 java 进程。jps 是包含在许多最新 JDK 中的实用程序。

回答by user5522

Try in a command prompt:

在命令提示符下尝试:

sc queryex type= service state= all | find "APP"

Where APP is the name of the program. This command will return all services that match that.

其中 APP 是程序的名称。此命令将返回匹配的所有服务。

Then you can run SC QUERYEX APPand it will return the state and PID number.

然后你可以运行SC QUERYEX APP,它会返回状态和PID号。

Once you have the PID:

一旦你有了PID:

TASKKILL /f /pid ###

Where ### is the actual PID

其中### 是实际的 PID

回答by Eric Leschinski

Java, get the PID of the current running process in Windows

Java,获取Windows中当前运行进程的PID

This should work on Linux, OSX, Windows, and HotSpot JVM's.

这应该工作在LinuxOSXWindows,和热点JVM的。

import sun.management.VMManagement;
import java.lang.management.ManagementFactory;
import java.lang.management.RuntimeMXBean;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public static int getCurrentPID() {
    try{
        java.lang.management.RuntimeMXBean runtime = 
            java.lang.management.ManagementFactory.getRuntimeMXBean();
        java.lang.reflect.Field jvm = runtime.getClass().getDeclaredField("jvm");
        jvm.setAccessible(true);
        sun.management.VMManagement mgmt = 
           (sun.management.VMManagement) jvm.get(runtime);
        java.lang.reflect.Method pid_method = 
            mgmt.getClass().getDeclaredMethod("getProcessId");
        pid_method.setAccessible(true);
        return (Integer) pid_method.invoke(mgmt);
    }
    catch(Exception e){
        e.printStackTrace();
        System.out.println("Failed at getting the process ID");
        System.exit(0);
    }
}

Invoke it like this:

像这样调用它:

System.out.println("PID: " + getCurrentPID());

For me it prints the processID: PID: 5728

对我来说,它打印 processID: PID: 5728

Sources:

资料来源:

How can a Java program get its own process ID?

Java 程序如何获得自己的进程 ID?

http://boxysystems.com/index.php/java-tip-find-process-id-of-running-java-process/

http://boxysystems.com/index.php/java-tip-find-process-id-of-running-java-process/

回答by ricardoespsanto

If you just need to kill a specific tomcat from a java application why not coding a simple servlet running inside each tomcat that will responde to a get request with a string that will identify it. Then use another servlet to execute something like:

如果您只需要从 Java 应用程序中杀死一个特定的 tomcat,为什么不编写一个在每个 tomcat 中运行的简单 servlet 来响应 get 请求并使用一个字符串来识别它。然后使用另一个 servlet 执行如下操作:

System.exit(-1);