javabatch-filewindows-8cmdwindows-8.1

Running jar file with argument from batch script windows 8


I am trying to run my .jar file with arguments through batch script in Windows 8.

My batch file (run.bat) has the following content:

set d=2015-07-07
java -jar my.jar %d%

But when I execute run.bat I get the following:

C:\Users\user\Desktop\Test>run.bat

C:\Users\user\Desktop\Test>set d=2015-07-07

C:\Users\user\Desktop\Test>java -jar my.jar

C:\Users\user\Desktop\Test>2015-07-07
'2015-07-07' is not recognized as an internal or external command,
operable program or batch file.

How to execute the whole command together with arguments?

When I run it without batch script through command line then it works:

C:\Users\user\Desktop\Test>java -jar my.jar 2015-07-07
C:\Users\user\Desktop\Test>

What I am doing wrong in a batch script?


Solution

  • Use batch code

    @echo off
    set "DateOption=2015-07-07"
    java.exe -jar my.jar %DateOption%
    set "DateOption="
    

    or perhaps even better

    @echo off
    set "DateOption=2015-07-07"
    "%JAVA_HOME%\bin\java.exe" -jar my.jar %DateOption%
    set "DateOption="
    

    According to commands output by cmd.exe on running your batch file, it looks like there is a character between my.jar and %d% in second line or between = and 2015-07-07 in first line being interpreted as newline by cmd.exe. It could be a single carriage return or a single line-feed. And a NULL (byte with value 0) is interpreted by cmd.exe as end of string which means also end of line.

    Batch files should have DOS/Windows line terminators, i.e. carriage return + line-feed. A line-feed only (UNIX) or carriage return only (old MAC) is not good for batch files.

    And batch files must be ASCII/ANSI (OEM) encoded files, i.e. 1 byte per character. Make sure not having UTF-8 or Unicode (UTF-16 Little/Big Endian) selected in the used text editor on using command Save As to save the batch file. Many text editors use by default nowadays UTF-8 as encoding which cmd.exe does not support.