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
Related
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.
I want to input a file into my jar after it is executed from batch file.
I wrote the following batch code.
#echo off
set path=%PATH%;C:\Program Files\Java\jre7\bin
java -jar E:\ER\er_v.3.3.17.jar
The above code is working fine. However, after the jar file is executed I need to feed another file for it to run successfully.
I want something like this
#echo off
set path=%PATH%;C:\Program Files\Java\jre7\bin
java -jar E:\ER\er_v.3.3.17.jar
echo E:\Run.xml
Can somebody help me with this?
Here you go. I tested this and it works.
#echo off
setlocal
:: if java.exe is not in %path%
for %%I in (java.exe) do if "%%~$PATH:I" equ "" (
set "PATH=%PATH%;C:\Program Files\Java\jre7\bin;C:\Program Files (x86)\Java\jre7\bin"
)
echo E:\Run.xml| java -jar E:\ER\er_v.3.3.17.jar
This passes "E:\Run.xml" to the stdin of java -jar etc.
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%
Alright i made a executable jar that holds a test game ran through a application. It runs fine for me but for my friend it does not. It says its unable to find the main class. So does he have to install java jdk or something? Is there a way around this?
Well, for one, he could be using the wrong command line arguments to the JVM to run the Java jar. Secondly, you could have not defined the main class correctly when you built the jar file (see: Unable to load main-class for jar file).
The way that you work around this? In the link I provided above, you'll see that there are a few command line parameters that can be supplied to the JVM that makes a big difference in how the JVM runs. Take those command line parameters and build a batch (or shell) file that end users can run to initialize the program correctly.
As your computer is setup differently, so may be the shell extension that handles files of type ".jar"
Try installing the newest version of java (JRE) on their machine (make sure it is of equal or higher version than the one your application was compiled against) and ensure that if you are using different architectures (x86 / x64) that you have the right versions of any libraries. I had this error only yesterday because I deployed a JDK 7u7 application to a machine with JRE 6u7.
Yes, there is a way around it. Run this script:
#ECHO off
SETLOCAL ENABLEDELAYEDEXPANSION
:: this .bat script creates a file association for executable .jar files
ECHO Creating .jar file association...
ECHO JAVA_HOME is %JAVA_HOME%
IF NOT DEFINED JAVA_HOME GOTO :FAIL
REG ADD "HKCR\jarfile" /ve /t REG_SZ /d "Executable Jar File" /f
REG ADD "HKCR\jarfile\shell" /ve /f
REG ADD "HKCR\jarfile\shell\open" /ve /f
ECHO REG ADD "HKCR\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%*" /f
REG ADD "HKCR\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%**" /f
REG ADD "HKLM\jarfile" /ve /t REG_SZ /d "Executable Jar File" /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell" /ve /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell\open" /ve /f
REG ADD "HKLM\SOFTWARE\Classes\jarfile\shell\open\command" /ve /t REG_SZ /d "\"%JAVA_HOME%\bin\javaw.exe\" -jar \"%%1\" %%*" /f
ECHO Finished creating .jar file association for executable .jar files.
PAUSE
GOTO EOF
:FAIL
ECHO Script failed. JAVA_HOME not defined.
PAUSE
install the new JRE .
while running the jar make sure you giv the class path in he command.
Eg. java -cp asd -jar file.jar