Adding java output to echo string of bat file - java

I need to assign Standard out of java application added to an echo string of a bat file. Lets say java application writes "hello world" via System.out.println.
For an example I have a bat file mybat.bat.
#echo off
echo output=java -jar E:\FYP\MyApp\out\artifacts\MyApp_jar\MyApp_jar\MyApp.jar %1
which does not give the output=Hello world
How can I achieve that. Your help is really appreciated.
Thank You !

As far as I understand you - you want to assign the output of the java to variable.
May be this will do this for you:
#echo off
set "java_output="
setlocal enableDelayedExpansion
for /f "delims=" %%J in ('java -jar E:\FYP\MyApp\out\artifacts\MyApp_jar\MyApp_jar\MyApp.jar %1') do (
set "java_output=!java_output! %%J"
)
endlocal & set java_output=%java_output%
echo %java_output%

You need to use for command to get the output of a program and assign it to a variable.
#echo off
for /F "tokens=*" %%o in ('java -jar E:\FYP\MyApp\out\artifacts\MyApp_jar\MyApp_jar\MyApp.jar %1') do set output=%%o
echo %output%

Related

Wmic process call in for loop doesn't start the cmd

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

Set Java Major Version Number As A Variable

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%

Command line options to batch file (Java application)

I've a java application which I run using a batch file- start.bat
Batch file:
#echo off
rem (
set JAVA_HOME=.\jre7
rem echo %JAVA_HOME%
java.exe -jar app-0.0.1-SNAPSHOT.jar
rem )
Manifest file has the name of class which has public static main(String[] args) method, and that's the start point of application.
I run the application from command prompt by typing start.bat
Now I need to give command line arguments to this bat file that can be received by main method.
so the task is to pass command line parameters to batch file and then send those parameter to java class main method.
I was exploring apache cli library but not sure if it can help me.
Basically this should be the input:
start.bat -a :
if -a is there then do task A in java application
start.bat -b :
if -b is there then do task B in java application
start.bat -a -b :
do task A and B
Help appreciated.
Thanks !
As user882813 points out above, the easiest solution is, if you want your .bat file to accept the same switches as your .jar file, just append %* (the batch variable containing all arguments) to the Java command like this:
java.exe jar app-0.0.1-SNAPSHOT.jar %*
If your .jar file can't accept the same switches for some reason, here's a way you can script a translation within your batch script. If you check for the existence of -a and -b within %*, you can construct your java launch command appropriately. This should work even if the switches are in backward order (like start.bat -b -a).
#echo off
setlocal
set "JAVA_HOME=.\jre7"
set a=& set b=& set "args=%*"
if "%*" neq "" (
if "%args%" neq "%args:-a=%" set "a=1"
if "%args%" neq "%args:-b=%" set "b=1"
)
if defined a (
if defined b (
rem :: Do both A and B
set "cmdArgs=/ABswitch"
) else (
rem :: Do work A
set "cmdArgs=/Aswitch"
)
) else if defined b (
rem :: Do work B
set "cmdArgs=/Bswitch"
) else (
rem :: Do no work
set cmdArgs=
)
java.exe -jar app-0.0.1-SNAPSHOT.jar %cmdArgs%
A quick note about the if "%args%" neq "%args:-a=%" set "a=1" lines: this works by variable substring substitution. Basically, %args:-a=% is %args% with -a replaced with nothing. So if %args% does not equal the same thing with -a removed, it must contain -a. Therefore, set a=1. See this page for more info on batch variable string manipulation.
Try something like the following in your batch script (after you have checked if those command line params are actually passed):
IF "%1" == "-a" GOTO DOWORKA
IF "%2" == "-b" GOTO DOWORKB
//as a third meassure , check for both -a and -b and invoke :DOWORKAB label
:DOWORKA
//invoke java program with param a
:DOWORKB
//invoke java program with param b
:DOWORKAB
//invoke java program with param a and b

Pipe multiple outputs of Java programs to multiple files

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.

Input filename into jar from windows batch file

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.

Categories