Find java PID using batch

僤鯓⒐⒋嵵緔 提交于 2019-12-24 06:50:24

问题


I need to know java process PID from Windows batch console.

@echo off
set p=%CD%
FOR /F "tokens=1" %%A IN ('"%JAVA_HOME%/bin/jps.exe -v"\|find  "%p%"') DO SET str=%%A
echo str = "%str%"

Java process unique identifier is path from what it was executed. Script executes jps, that returns all java process information, for example

9376 Jps -Denv.class.path=D:\tools\timesten\lib\ttjdbc6.jar; -Dapplication.home=C:\Program Files\Java\jdk1.6.0_24 -Xms8m
3856  -Dexe4j.semaphoreName=c:_program files (x86)_jetbrains_intellij idea community edition 12.0.1_bin_idea.exe -Dexe4j.moduleName=C:\Program Files (x86)\JetBrains\IntelliJ IDEA Community Edition 12....etc

Batch says that: | was unexpected at this time.

Could you please said , how to correctly extract PID.


回答1:


You have to escape the pipe like this ^| within a FOR statement, otherwise it tries to pipe the first half of the FOR statement into the second.

Also this is how I would find a PID.

for /f "tokens=2" %%a in ('tasklist ^| find "jps.exe") do set javapid=%%a




回答2:


You can user command "tasklist" for show PID。




回答3:


Correct script

   "%JAVA_HOME%/bin/jps.exe" -v>temp.txt
   for /f "tokens=1" %%f in ('find "%CD%" "temp.txt"') do set str=%%f
   echo str=%str%


来源:https://stackoverflow.com/questions/14029591/find-java-pid-using-batch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!