<p>I need a Java way to find a running Win process from which I know to name of the executable. I want to look whether it is running right now and I need a way to kill the process if I found it.</p>

[{'answer_id': 81948, 'author': 'jb.', 'author_id': 7918, 'author_profile': 'https://Stackoverflow.com/users/7918', 'pm_score': 0, 'selected': False, 'text': '<p>You will have to call some native code, since IMHO there is no library that does it. Since JNI is cumbersome and hard you might try to use JNA (Java Native Access). <a href="https://jna.dev.java.net/" rel="nofollow noreferrer">https://jna.dev.java.net/</a></p>\n'}, {'answer_id': 81974, 'author': 'arturh', 'author_id': 4186, 'author_profile': 'https://Stackoverflow.com/users/4186', 'pm_score': 2, 'selected': False, 'text': '<p>You could use a command line tool for killing processes like <a href="http://technet.microsoft.com/de-de/sysinternals/bb896683.aspx" rel="nofollow noreferrer">SysInternals PsKill</a> and <a href="http://technet.microsoft.com/de-de/sysinternals/bb896682.aspx" rel="nofollow noreferrer">SysInternals PsList</a>.</p>\n\n<p>You could also use the build-in tasklist.exe and taskkill.exe, but those are only available on Windows XP Professional and later (not in the Home Edition).</p>\n\n<p>Use <a href="http://java.sun.com/javase/6/docs/api/java/lang/Runtime.html#exec(java.lang.String)" rel="nofollow noreferrer">java.lang.Runtime.exec</a> to execute the program.</p>\n'}, {'answer_id': 82035, 'author': 'Community', 'author_id': -1, 'author_profile': 'https://Stackoverflow.com/users/-1', 'pm_score': 5, 'selected': True, 'text': '<p>You can use command line windows tools <code>tasklist</code> and <code>taskkill</code> and call them from Java using <code>Runtime.exec()</code>.</p>\n'}, {'answer_id': 2667948, 'author': 'Daniel Lindner', 'author_id': 320402, 'author_profile': 'https://Stackoverflow.com/users/320402', 'pm_score': 2, 'selected': False, 'text': '<p>There is a little API providing the desired functionality:</p>\n\n<p><a href="https://github.com/kohsuke/winp" rel="nofollow noreferrer">https://github.com/kohsuke/winp</a></p>\n\n<p>Windows Process Library</p>\n'}, {'answer_id': 7473589, 'author': '1-14x0r', 'author_id': 334927, 'author_profile': 'https://Stackoverflow.com/users/334927', 'pm_score': 6, 'selected': False, 'text': '<pre><code>private static final String TASKLIST = "tasklist";\nprivate static final String KILL = "taskkill /F /IM ";\n\npublic static boolean isProcessRunning(String serviceName) throws Exception {\n\n Process p = Runtime.getRuntime().exec(TASKLIST);\n BufferedReader reader = new BufferedReader(new InputStreamReader(\n   p.getInputStream()));\n String line;\n while ((line = reader.readLine()) != null) {\n\n  System.out.println(line);\n  if (line.contains(serviceName)) {\n   return true;\n  }\n }\n\n return false;\n\n}\n\npublic static void killProcess(String serviceName) throws Exception {\n\n  Runtime.getRuntime().exec(KILL + serviceName);\n\n }\n</code></pre>\n\n<p>EXAMPLE:</p>\n\n<pre><code>public static void main(String args[]) throws Exception {\n String processName = "WINWORD.EXE";\n\n //System.out.print(isProcessRunning(processName));\n\n if (isProcessRunning(processName)) {\n\n  killProcess(processName);\n }\n}\n</code></pre>\n'}, {'answer_id': 29413515, 'author': 'Craig', 'author_id': 529256, 'author_profile': 'https://Stackoverflow.com/users/529256', 'pm_score': 2, 'selected': False, 'text': '<p>Here\'s a groovy way of doing it:</p>\n\n<pre><code>final Process jpsProcess = "cmd /c jps".execute()\nfinal BufferedReader reader = new BufferedReader(new InputStreamReader(jpsProcess.getInputStream()));\ndef jarFileName = "FileName.jar"\ndef processId = null\nreader.eachLine {\n    if (it.contains(jarFileName)) {\n        def args = it.split(" ")\n        if (processId != null) {\n            throw new IllegalStateException("Multiple processes found executing ${jarFileName} ids: ${processId} and ${args[0]}")\n        } else {\n            processId = args[0]\n        }\n    }\n}\nif (processId != null) {\n    def killCommand = "cmd /c TASKKILL /F /PID ${processId}"\n    def killProcess = killCommand.execute()\n    def stdout = new StringBuilder()\n    def stderr = new StringBuilder()\n    killProcess.consumeProcessOutput(stdout, stderr)\n    println(killCommand)\n    def errorOutput = stderr.toString()\n    if (!errorOutput.empty) {\n        println(errorOutput)\n    }\n    def stdOutput = stdout.toString()\n    if (!stdOutput.empty) {\n        println(stdOutput)\n    }\n    killProcess.waitFor()\n} else {\n    System.err.println("Could not find process for jar ${jarFileName}")\n}\n</code></pre>\n'}, {'answer_id': 30458368, 'author': 'Harvendra', 'author_id': 3343726, 'author_profile': 'https://Stackoverflow.com/users/3343726', 'pm_score': 0, 'selected': False, 'text': '<p>small change in answer written by Super kakes</p>\n\n<pre><code>private static final String KILL = "taskkill /IMF ";\n</code></pre>\n\n<p>Changed to ..</p>\n\n<pre><code>private static final String KILL = "taskkill /IM ";\n</code></pre>\n\n<p><code>/IMF</code> option doesnot work .it does not kill notepad..while <code>/IM</code> option actually works</p>\n'}, {'answer_id': 30843674, 'author': 'BullyWiiPlaza', 'author_id': 3764804, 'author_profile': 'https://Stackoverflow.com/users/3764804', 'pm_score': 1, 'selected': False, 'text': '<p>Use the following class to <a href="https://en.wikipedia.org/wiki/Kill_(command)#Microsoft_Windows" rel="nofollow">kill a Windows process</a> (<a href="https://en.wikipedia.org/wiki/Tasklist" rel="nofollow">if it is running</a>). I\'m using the force command line argument <code>/F</code> to make sure that the process specified by the <code>/IM</code> argument will be terminated.</p>\n\n<pre><code>import java.io.BufferedReader;\nimport java.io.InputStreamReader;\n\npublic class WindowsProcess\n{\n    private String processName;\n\n    public WindowsProcess(String processName)\n    {\n        this.processName = processName;\n    }\n\n    public void kill() throws Exception\n    {\n        if (isRunning())\n        {\n            getRuntime().exec("taskkill /F /IM " + processName);\n        }\n    }\n\n    private boolean isRunning() throws Exception\n    {\n        Process listTasksProcess = getRuntime().exec("tasklist");\n        BufferedReader tasksListReader = new BufferedReader(\n                new InputStreamReader(listTasksProcess.getInputStream()));\n\n        String tasksLine;\n\n        while ((tasksLine = tasksListReader.readLine()) != null)\n        {\n            if (tasksLine.contains(processName))\n            {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    private Runtime getRuntime()\n    {\n        return Runtime.getRuntime();\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/81902', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/11705/']