0

I need to run a java jar file in a bat file in a for loop based on input from a file, each line in the file contains an argument:

SET jar-path="C:\\tools\\myJar.jar"
SET out-path=C:\tmp\out
SET args-file=C:\data\args.txt

for /f "usebackq" %%a in ("%args-file%") do (
    java -jar %jar-path% %%a %out-path%
    :: start /wait java -jar %jar-path% %%a %out-path%
)

Where args.txt contains:

a
b
c

But the loop just iterates 3 times without running the jar file. If I do it outside the loop it works, any suggestions?

u123
  • 15,603
  • 58
  • 186
  • 303

1 Answers1

0

What about using following batch code:

set "jar-path=C:\tools\myJar.jar"
set "out-path=C:\tmp\out"
set "args-file=C:\data\args.txt"

for /F "usebackq delims=" %%a in ("%args-file%") do (
    "java.exe" -jar "%jar-path%" %%a "%out-path%"
)

It makes a huge difference where the double quotes are used on assigning a string to an environment variable, see for example Assign string values with space to environment variables right for an explanation.

It would be also best to specify java.exe with full path if that is possible because batch file is used always on same computer. In batch files it should be avoided that command line interpreter has to use the environment variables PATHEXT and PATH to find out which application to run.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143