问题
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