How to find the process id of a running Java process on Windows? And how to kill the process alone?

后端 未结 8 1707
庸人自扰
庸人自扰 2021-01-30 21:59

I want to kill the particular Java process in Windows, like in Linux (ps -aux to get processid and then kill processid to kill the process).

相关标签:
8条回答
  • 2021-01-30 22:27

    The solution I found is very simple. Use Window's WMIC & Java's Runtime to locate & kill the process.

    Part 1: You need to put some sort of identifier into your app's startup command line. E.g. something like:

    String id = "com.domain.app";
    

    Part 2: When you run your app, make sure to include the string. Let's say you start it from within Java, do the following:

    Runtime.getRuntime().exec(
       "C:\...\javaw.exe -cp ... -Dwhatever=" + id + " com.domain.app.Main"
    );
    

    Part 3: To kill the process, use Window's WMIC. Just make sure you app was started containing your id from above:

    Runtime.getRuntime().exec(
      "wmic process Where \"CommandLine Like '%" + id + "%'\" Call Terminate"
    );
    
    0 讨论(0)
  • 2021-01-30 22:27
    1. Open Git Bash
    2. Type ps -ef | grep java
    3. Find the pid of running jdk
    4. kill -9 [pid]
    0 讨论(0)
提交回复
热议问题