我们如何通过 Windows cmd 停止正在运行的 java 进程?

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

How can we stop a running java process through Windows cmd?

javawindowsbatch-filecmd

提问by Winston Chen

I am a newbie in cmd, so please allow me to ask a stupid question: How can we stop a running Java process through Windows cmd?

我是 cmd 的新手,所以请允许我问一个愚蠢的问题:我们如何通过 Windows cmd 停止正在运行的 Java 进程?

For example, if we start Jetty (a mini web server) with the following command:

例如,如果我们使用以下命令启动 Jetty(一个迷你 Web 服务器):

start javaw -jar start.jar

How do we find the process and stop it later?

我们如何找到进程并在以后停止它?

Obviously the following command does not work:

显然以下命令不起作用:

stop javaw -jar start.jar

采纳答案by Stephen C

It is rather messy but you need to do something like the following:

它相当混乱,但您需要执行以下操作:

START "do something window" dir
FOR /F "tokens=2" %I in ('TASKLIST /NH /FI "WINDOWTITLE eq do something window"' ) DO SET PID=%I
ECHO %PID%
TASKKILL /PID %PID%

Found this on this page.

这个页面上找到了这个

(This kind of thing is much easier if you have a UNIX / LINUX system ... or if you run Cygwin or similar on Windows.)

(如果您有 UNIX / LINUX 系统……或者如果您在 Windows 上运行 Cygwin 或类似系统,这种事情会容易得多。)

回答by Joey

Normally I don't have that many Java processes open so

通常我没有那么多 Java 进程打开所以

taskkill /im javaw.exe

or

或者

taskkill /im java.exe

should suffice. This will kill allinstances of Java, though.

应该足够了。但是,这将杀死所有Java 实例。

回答by vneste

FOR /F "tokens=1,2 delims= " %%G IN ('jps -l') DO IF %%H==name.for.the.application.main.Class taskkill /F /PID %%G

name.for.the.application.main.Class- replace this to your application's main class (you can find it in second column of jps -loutput)

name.for.the.application.main.Class- 将其替换为应用程序的主类(您可以在jps -l输出的第二列中找到它)

回答by Tup

When I ran taskkill to stop the javaw.exe process it would say it had terminated but remained running. The jqs process (java qucikstart) needs to be stopped also. Running this batch file took care of the issue.

当我运行 taskkill 来停止 javaw.exe 进程时,它会说它已终止但仍在运行。jqs 进程 (java qucikstart) 也需要停止。运行这个批处理文件可以解决这个问题。

taskkill /f /im jqs.exe
taskkill /f /im javaw.exe
taskkill /f /im java.exe

回答by rojo

I like this one.

我喜欢这个。

wmic process where "name like '%java%'" delete

You can actually kill a process on a remote machine the same way.

您实际上可以以相同的方式终止远程计算机上的进程。

wmic /node:computername /user:adminuser /password:password process where "name like '%java%'" delete

wmic is awesome!

wmic 太棒了!

回答by ffoooo

start javaw -DSTOP.PORT=8079 -DSTOP.KEY=secret -jar start.jar

启动 javaw -DSTOP.PORT=8079 -DSTOP.KEY=secret -jar start.jar

start javaw -DSTOP.PORT=8079 -DSTOP.KEY=secret -jar start.jar --stop

启动 javaw -DSTOP.PORT=8079 -DSTOP.KEY=secret -jar start.jar --stop

回答by Rosberg Linhares

You can do this with PowerShell:

您可以使用 PowerShell 执行此操作:

$process = Start-Process "javaw" "-jar start.jar" -PassThru
taskkill /pid $process.Id

The taskkillcommand will graceful close the application.

taskkill命令将正常关闭应用程序。

回答by Vinaykumar Patel

In case you want to kill not all java processes but specif jars running. It will work for multiple jars as well.

如果您不想杀死所有的 java 进程,而是想杀死正在运行的特定 jar。它也适用于多个罐子。

wmic Path win32_process Where "CommandLine Like '%YourJarName.jar%'" Call Terminate

Else taskkill /im java.exewill work to kill all java processes

否则taskkill /im java.exe将杀死所有 Java 进程

回答by sgrillon

(on Windows OS without Service) Spring Boot start/stop sample.

(在没有服务的 Windows 操作系统上)Spring Boot 启动/停止示例。

run.bat

运行.bat

@ECHO OFF
IF "%1"=="start" (
    ECHO start your app name
    start "yourappname" java -jar -Dspring.profiles.active=prod yourappname-0.0.1.jar
) ELSE IF "%1"=="stop" (
    ECHO stop your app name
    TASKKILL /FI "WINDOWTITLE eq yourappname"
) ELSE (
    ECHO please, use "run.bat start" or "run.bat stop"
)
pause

start.bat

启动文件

@ECHO OFF
call run.bat start

stop.bat:

停止.bat:

@ECHO OFF
call run.bat stop

回答by tk_

Open the windows cmd.First list all the java processes,

打开windows cmd。首先列出所有的java进程,

jps -m

now get the name and run below command,

现在获取名称并运行以下命令,

for /f "tokens=1" %i in ('jps -m ^| find "Name_of_the_process"') do ( taskkill /F /PID %i )

or simply kill the process ID

或者干脆杀死进程ID

taskkill /F /PID <ProcessID>

sample :)

样本 :)

C:\Users\tk>jps -m
15176 MessagingMain
18072 SoapUI-5.4.0.exe
15164 Jps -m
3420 org.eclipse.equinox.launcher_1.3.201.v20161025-1711.jar -os win32 -ws win32 -arch x86_64 -showsplash -launcher C:\Users\tk\eclipse\jee-neon\eclipse\eclipse.exe -name Eclipse --launcher.library C:\Users\tk\.p2\pool\plugins\org.eclipse.equinox.launcher.win32.win32.x86_64_1.1.401.v20161122-1740\eclipse_1617.dll -startup C:\Users\tk\eclipse\jee-neon\eclipse\plugins/org.eclipse.equinox.launcher_1.3.201.v20161025-1711.jar --launcher.appendVmargs -exitdata 4b20_d0 -product org.eclipse.epp.package.jee.product -vm C:/Program Files/Java/jre1.8.0_131/bin/javaw.exe -vmargs -Dosgi.requiredJavaVersion=1.8 -XX:+UseG1GC -XX:+UseStringDeduplication -Dosgi.requiredJavaVersion=1.8 -Xms256m -Xmx1024m -Declipse.p2.max.threads=10 -Doomph.update.url=http://download.eclipse.org/oomph/updates/milestone/latest -Doomph.redirection.index.redirection=index:/->http://git.eclipse.org/c/oomph/org.eclipse.oomph.git/plain/setups/ -jar C:\Users\tk\

and

C:\Users\tk>for /f "tokens=1" %i in ('jps -m ^| find "MessagingMain"') do ( taskkill /F /PID %i )

C:\Users\tk>(taskkill /F /PID 15176  )
SUCCESS: The process with PID 15176 has been terminated.

or

或者

C:\Users\tk>taskkill /F /PID 15176 
SUCCESS: The process with PID 15176 has been terminated.