Command line options to batch file (Java application) - java

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

Related

execute bat with java program in task scheduler fail

I have the following script, which works fine if executed manually, but does not work if set as a task in the task scheduler.
I suspected a parameter may not be set correctly, but cannot spot it. Could someone please help me with that.
#echo off
setlocal EnableDelayedExpansion
cls
set AllSections=
Set Action=
rem set PARMS=-Xms64M -Xmx512M -Dfile.encoding=UTF-8 -Dsikuli.FromCommandLine
rem set JAVA_HOME=C:\Program Files\Java\jdk1.8.0_202
rem set SIKULIX_HOME=%~dp0
set CurrentDate=%Date%
set CurrentTime=%Time: =0%
set CurrentTime=%CurrentTime:~0,8%
set DateTimeStamp=%CurrentDate:~6,4%-%CurrentDate:~3,2%-%CurrentDate:~0,2%_%CurrentTime:~0,2%-%CurrentTime:~3,2%-%CurrentTime:~6,2%
set FolderTimeStamp=%CurrentDate:~3,2%-%CurrentDate:~6,4%
set DestinationFolder=SCADA\Output\%FolderTimeStamp%\%DateTimeStamp%
md DestinationFolder=SCADA\Output > nul 2>&1
md DestinationFolder=SCADA\Output\%FolderTimeStamp% > nul 2>&1
md DestinationFolder=SCADA\Output\%FolderTimeStamp%\%DateTimeStamp% > nul 2>&1
md DestinationFolder=SCADA\Output\%FolderTimeStamp%\%DateTimeStamp%\Settings > nul 2>&1
md DestinationFolder=SCADA\Output\%FolderTimeStamp%\%DateTimeStamp%\Alarms > nul 2>&1
md DestinationFolder=SCADA\Output\%FolderTimeStamp%\%DateTimeStamp%\Network > nul 2>&1
rem Create Settings file
rem echo "Hello world" > %cd%\aa.txt
rem \\\--below instructions working as script from command prompt , but not from task scheduler
C:\sikulix\sikulixapi-2.0.5-win.jar -r C:\sikulix\Scada\Settings.sikuli
C:\sikulix\sikulixapi-2.0.5-win.jar -r C:\sikulix\Scada\Alarms.sikuli
C:\sikulix\sikulixapi-2.0.5-win.jar -r C:\sikulix\Scada\Networks.sikuli
rem /////
ENDLOCAL
:End
I highlighted the bit that does not work; the folders are set correctly, but the java part fails to be executed in the task scheduler.
The script runs in your account because you have set up Windows such that the file extension .jar mapped to the java application. If the task scheduler account doesn't map .jar to java application then it won't work.
Re-write the script to prefix the lines using jars with a valid Java runtime such as %JAVA_HOME%/bin/java and ensure that JAVA_HOME is defined, or check that the task scheduler account maps .jar handling to a suitable JVM. Example:
xyz.jar param1 param2 ...
becomes
%JAVA_HOME%\bin\java --class-path xyz.jar param1 param2 ..
In Windows Explorer, you can check the mappings with right mouse -> Properties then see "Open with:" field.

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 %*

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 .

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.

ArrayIndexOutOfBounds Exception on executing linux sh file

I have a program in java which takes 0'th aargument as file location like
File f = new File(args[0]);
so when i execute it using a windows batch(.bat) file it works correctly .
but when i execute the same using a linux shell file(.sh) in linux i get ArrayIndexOutOfBoundsException.
WINDOWS BATCH FILE :
#echo off
for /f %%i in ("%0") do set scriptpath=%%~dpi
set cp=%scriptpath%/../lib/*.jar;
java -classpath %cp% com.synchronizer.main.MYSynchronizer %scriptpath% "%1" "%2"
LINUX SH FILE:
export JAVA_HOME=/usr/local/java
PATH=/usr/local/java/bin:${PATH}
THE_CLASSPATH=
for i in `ls ../lib/*.jar`
do
THE_CLASSPATH=${THE_CLASSPATH}:${i}
done
java -cp ".:${THE_CLASSPATH}" \
com.synchronizer.main.MYSynchronizer
please help!
It looks like a problem in script (no arguments are passed to the Java program).
You can consider to debug the script like this: debugging scripts
Hope this helps
Your shell script is not passing any parameters:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer
Try:
java -cp ".:${THE_CLASSPATH}" com.synchronizer.main.MYSynchronizer "$1" "$2"
As stated above, your Linux shell script is not sending any arguments to the Java program that you are trying to start.
And, adding to that, you are not showing us how you run the Linux shell script. If no argument is given on the command line when you start the shell script, no arguments can be passed to your Java application from the shell script.
If you want to see the actual command that is going to be run by your shell script, you can always put "echo" in front of a line and see what all variables are expanded to. This is a simple way to debug shell scripts.

Categories