Input filename into jar from windows batch file - java

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.

Related

BASH to BATCH java

There's this BASH file:
#!/bin/bash
set -eu
DIR=`dirname $0`
JAR=$DIR/myjar.jar
CLASSPATH=$JAR:./
HEAP_SIZE=-size
java $HEAP_SIZE -cp $CLASSPATH something.something2 "$#"
That I want to turn into a .bat file to run on windows
This is what I have so far:
#ECHO OFF
set DIR = %cd%
set JAR = DIR/myjar.jar
set CLASSPATH = %JAR%:./
set HEAP_SIZE = -size
java %HEAP_SIZE% -cp %CLASSPATH% something.something2
How would I complete it to have the same behavior as the bash file?
%CD% can work, but your bash script seems to be setting the directory based off the path of the script (i.e., argument in $0) and not the current directory. To use the same directory as the batch file, you use %~dp0.
You missed % around your DIR variable when you wanted to expand the value.
You shouldn't have spaces around equal signs in your set statements.
The equivalent of $# in batch is %*.
You should quote your %CLASSPATH%. On Windows you are much more likely to encounter paths with spaces in them.
#ECHO OFF
set "DIR=%~dp0"
set "JAR=%DIR%/myjar.jar"
set "CLASSPATH=%JAR%:./"
set "HEAP_SIZE=-size"
java %HEAP_SIZE% -cp "%CLASSPATH%" something.something2 %*

Use Windows Batch File to start a Java program

I am not familiar with batch script, but I want to create a Windows Batch File to start a Java program. The problem is that it has to specific the path where JRE is installed. When you install both JRE7 and JRE8, the name of that JRE8 folder would call something like jre1.8.0_20 or jre1.8.0_40 with the version number in the back. When you have only JRE8 installed, the folder would call jre8. Is there an easier way to find where the most updated JRE installed and then execute it? Thanks.
start ..\..\Java\jre7\bin\javaw.exe -Xms512M -Xmx1024M -Djna.library.path=.\lib -cp example.jar; com.example.main
You should be able to get the location of javaw.exe by executing where java. This can be set as a variable inside a batch file like this:
# sets a variable called 'java' to the location of javaw.exe
for /f "delims=" %a in ('where javaw') do #set java=%a
# execute you jar file
%java% -jar <app.jar>
Noticed that the above only seems to work when running directly from the command line. Here is another example that should work in a batch file:
# run.bat
#echo off
setlocal enabledelayedexpansion
for /f %%a in ('where javaw') do (
set java=%%a
)
!java! -jar %1
The above batch file should be called with the name of the jar file:
run.bat app.jar
I think it's best to just user JAVA_HOME and/or JRE_HOME and let the user / sysadmin worry what's installed.

-I flag JAVA command # .bat CMAKE

I have to extract and generate some .h files from a jar and idl file.
Need to use a .bat from external developers:
#echo off
java -jar C:\WorkspaceSwim\trunk\iB\build\BlueGen\target\bluegen.jar -o C:\WorkspaceSwim\trunk\Example\Distribution\build\GeneratedDDSFiles C:\WorkspaceSwim\trunk\Example\Distribution\Flight.idl
set NDDSHOME=C:\WorkspaceSwim\trunk\iB\build\ToolBox\NDDS
set RPCDDSHOME=C:\WorkspaceSwim\trunk\iB\build\ToolBox\RPCDDS
setlocal
set dir=%~dp0
set args=%1
:getarg
shift
if "%~1"=="" goto continue
set args=%args% %1
goto getarg
:continue
set JREHOME=%JAVA_HOME%
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME% /idl"
I think there are some fail on the script , but i can't see it. Maybe a problem with quotes? i tryied this 3 posibilities :
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME%\idl"
Error: principal class \idl doesn't found or load
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes" com.eprosima.rpcdds.idl.RPCDDSGEN %args% "-I%RPCDDSHOME%\idl"
Error: principal class com.eprosima.rpcdds.idl.RPCDDSGEN doesn't found or load
"%JREHOME%\bin\java" -DPATH="%PATH%" -DNDDSHOME="%NDDSHOME%" -DRPCDDSHOME="%RPCDDSHOME%" -Djava.ext.dirs="%RPCDDSHOME%\classes\ com.eprosima.rpcdds.idl.RPCDDSGEN %args%" -I "%RPCDDSHOME%\idl"
Unrecognized option: -I
Error: Could not create the Java Virtual Machine.
Previusly i use this .cmake to generate the code that will run this .bat
add_custom_command(
OUTPUT ${DDSGeneratedFiles}
COMMAND java -jar ${iB_BlueGen} -o ${GeneratedDDSDirName} ${LocatedFile}
COMMAND "set NDDSHOME=${iMASBlue_NDDS_DIR} &" "set RPCDDSHOME=${iB_RPCDDS_DIR} &" ${RPCDDSGenerator} -replace -d ${GeneratedDDSDirName} -I${CMAKE_CURRENT_SOURCE_DIR} ${LocatedFile}
DEPENDS ${IncludeDependencies}
MAIN_DEPENDENCY ${LocatedFile}
COMMENT "Calling RPCDDS Generator '${RPCDDSGenerator}' on '${IdlFile}'"
)
Thnx for your answers.
To debug batch files, change line
#echo off
in your batch file to
#echo on
and run again

Adding java output to echo string of bat file

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%

Windows Batch File popd not working as expected

I am using the following batch script to run a Java Command Line tool.
#echo off
pushd %~dp0
setLocal EnableDelayedExpansion
set CLASSPATH="
for /R ./libs %%a in (*.jar) do (
set CLASSPATH=!CLASSPATH!;%%a
)
set CLASSPATH=!CLASSPATH!"
java -cp !CLASSPATH! com.example.CLIApplication %*
popd
I have added the tool's directory to the System Variables PATH so that I can run it from any directory via the command prompt. This is working but the problem I am seeing is:
The tool's dir is C:\tool\
The user is in C:\
After executing the batch file the user is left in C:\tool\ not C:\
popd is getting called but the console navigates back to C:\too\ instead of staying in C:\
How do I ensure they users directory is not changed after the script finishes?
The setlocal without endlocal causes this problem here.
You only need to add an endlocal just before calling popd.
In your code the popd returns to your first directory, but as setlocal stores all variables and all open setlocals are closed by implicit endlocals when the batch is exited, it will also restore the cd variable.
I may be off track, but why aren't you using:
pushd .

Categories