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.
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
You can user command "tasklist" for show PID。
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%
Related
I would like to kill a process using java on windows by it's parameters.
If I have an exe and I run it using "exe param1 param2 etc.." and let's say param1 is unique but exe is not, I woulk like to kill the process by param1.
How can I do that ?
You could use following command to find the java process id then use taskkill to kill the process or use cygwin in windows and use linux command to kill the process hth
jps -lv | findstr "STRING"
You could use following command to find the tasklist with commandline column
wmic path win32_process get | findstr java.exe
I am not sure what do you mean.
If you want to kill the process in Windows by command in Java you need to type this:
Runtime.getRuntime().exec("taskkill /F <processname>.exe")
It executes the Windows command passed as the string of the method of Runtime class.
taskkill - command ends one or more tasks or processes.
/F - parameter specifies process to be terminated
target process started via command line : notepad killme.txt"
for /f "tokens=1,2" %i in ('wmic path win32_process get Commandline') do (IF "%j" == "killme.txt" (taskkill /FI "IMAGENAME eq %i*"))
I am trying to retrieve the ProcessID of a Java app launched through a batch file. The problem is that the application does not start.
My code:
set cmd=java -jar XXXXXXX.jar XXXXXX.yml
for /f "tokens=2 delims==; " %%a in (' wmic process call create "%cmd%" ^| find "ProcessId" ') do set PID=%%a
start cmd /k echo %pid%
PAUSE
Batch files are pretty picky with whitespace.
Change this:
set cmd = java -jar XXXXXXX.jar XXXXXX.yml
To this:
set cmd=java -jar XXXXXXX.jar XXXXXX.yml
This works:
set cmd=notepad
for /f "tokens=2 delims==; " %%a in (' wmic process call create "%cmd%" ^| find "ProcessId" ') do set PID=%%a
start cmd /k echo %pid%
PAUSE
There may be something wrong with your jar file or perhaps java is not on your PATH. Try running your command directly at a command prompt to see if it works:
c:\>java -jar XXXXXXX.jar XXXXXX.yml
Also make sure that you use the full path to your jar file in your cmd otherwise the path will be relative to your system folder:
set cmd=java -jar c:\yourpath\XXXXXXX.jar XXXXXX.yml
This is irritating. I am trying to incorporate a trigger in a script to run if the major version of Java is 1.6,1.7, etc.
In the line I am trying to set the variable in, I pull the Java version and pipe it to for with tokens and delims identified, resulting in the "do" as a set command for variable %jver%. However, echoing %jver% results in "echo is on". Why wont it set the variable? Everything looks legit until %jver% is used.
Yes, I double the percentages for the script. The code here is for use at the command prompt.
Here is the line:
%systemroot%\system32\java.exe -version 2>&1 | for /f "tokens=1-4 delims=. " %a in ('findstr /i "version"') do (set jver=%~c.%d)
#rao thank you for helping me discover an alternate solution to meet my intent.
My workaround solution to this is the following line:
for /f "tokens=1-4 delims=. " %a in ('java -version 2^>^&1 ^| findstr /i "version"') do (SET JVER=%~c.%d)
Looks this is answered in this post by Patrick Cuff
Here is the solution adding from mentioned original post
#echo off
setlocal
set VERSION6="1.6.0_21"
for /f "tokens=3" %%g in ('java -version 2^>^&1 ^| findstr /i "version"') do (
#echo Output: %%g
set JAVAVER=%%g
)
set JAVAVER=%JAVAVER:"=%
#echo Output: %JAVAVER%
I'm using the following code to check whether there is a java process with the specific name.
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
how can I use this as an if condition rather than iterating. and this command exits the batch code even if there is no process named such. is there any thing wrong with this code.
This is my entire batch script
start start-chrome.bat
for /f "tokens=1" %i in ('jps -m ^| find "TouchPosApplication"') do ( EXIT /B )
ant main
set var=False
start start-chrome.bat
for /f "tokens=1" %%i in ('jps -m ^| find "TouchPosApplication"') do set var=True
if "%var%"=="True" (
Echo Found
) else (
ant main
)
I'm on phone so can't test it but
goto label
will jump to and execute ant main whereas goto finish would end program.
label:
ant main
finish:
PS. I don't recall batch accepting single quotes, which are apostrophes really.
I have a set of files, labeled file1.java, file2.java, ...
I want to pipe the output of the files to .txt files, such as output1.txt, output2.txt, etc. corresponding to each java file.
I know that I can do this by doing:
javac file1.java
java file1 > output1.txt
However, is there a way to do this of all files in a given directory? (instead of doing it manually)
This is a good for loop example:
Command line usage:
for /f %f in ('dir /b c:\') do echo %f
Batch file usage:
for /f %%f in ('dir /b c:\') do echo %%f
if the directory contains files with space in the names, you need to change the delimiter the for /f command is using. for example, you can use the pipe char.
for /f "delims=|" %%f in ('dir /b c:\') do echo %%f
If the directory name itself has a space in the name, you can use the usebackq option on the for:
for /f "usebackq delims=|" %%f in (`dir /b "c:\program files"`) do echo %%f
And if you need to use output redirection or command piping, use the escape char (^):
for /f "usebackq delims=|" %%f in (dir /b "c:\program files" ^|
findstr /i microsoft) do echo %%f
It shows both command line + batch for loop examples. Then you will have the filenames and can just run some commands on those filenames like javac and java.
Just try to break it down to small tasks:
Get list of files (all or just .class)
Loop through each file and call javac and java on them
Google is your best friend - search for "DOS batch" + what you need
Your job is much easier if you restructure your output file names a bit: file1.java --> file1_output.txt
From the command line:
for %F in (*.java) do #javac "%F" >"%~nF_output.txt"
If in a batch file:
#echo off
for %%F in (*.java) do javac "%%F" >"%%~nF_output.txt"
If you keep your original naming convention, then you must parse out the number form each file name. Very doable, but it doesn't seem worthwhile in your case.