1

Given this file myfile.txt:

-Dvariable=a -Dvariable2=b -Dvariable3=c
-Dvariable=a -Dvariable2=b -Dvariable3=d
-Dvariable=a -Dvariable2=b -Dvariable3=e

and batch.bat:

for /F "tokens=*" %%A in (myfile.txt) do (
    C:\javapath\java.exe %%A -XX:..... -classpath....... main.Main
)

How can I make %%A dereference as program arguments?

With the above, the output is: (i.e. its just stopping after java.exe)

(C:\javapath\java.exe)
Usage: java [-options] class [args...] //etc....

With doing set first, I simply get spaces:

batch.bat:
set x=%%A
C:\javapath\java.exe %x% -XX:..... -classpath....... main.Main

output:
set x=-Dvariable=a -Dvariable2=b -Dvariable3=c
C:\javapath\java.exe  -XX:..... -classpath....... main.Main
CeePlusPlus
  • 803
  • 1
  • 7
  • 26
  • Do not assign the string value assigned to a loop variable to an environment variable within a __FOR__ loop on not modifying the string value next. Please open a [command prompt](https://www.howtogeek.com/235101/), run `set /?` and read the output help explaining the reason as well as [Variables are not behaving as expected](https://stackoverflow.com/questions/30282784/). Run `for /?` for help/manual/documentation of command __FOR__. Don't use `A` as loop variable although it is possible. After reading about `%~aI` and the other modifiers you can perhaps imagine why `A` or `a` is not so good. – Mofi Feb 14 '20 at 17:20
  • 1
    The command line `for /F "usebackq delims=" %%I in ("myfile.txt") do "C:\javapath\java.exe" %%I -XX:..... -classpath....... main.Main` should work as long as no line in the text file has default end of line character `;` as first character and `myfile.txt` is in current directory whatever is the current directory. I recommend to put the command `echo` after `do` and run the batch file from within a command prompt window to see the command line which would be executed with `java.exe`, see also [debugging a batch file](https://stackoverflow.com/a/42448601/3074564). – Mofi Feb 14 '20 at 17:26

1 Answers1

0

With thanks to @Mofi.

for /F "usebackq delims=" %%I in (myfile.txt) do (
    C:\javapath\java.exe %%I -XX:..... -classpath....... main.Main
)
CeePlusPlus
  • 803
  • 1
  • 7
  • 26
  • __Note:__ The option `usebackq` is only needed if the file name of the text file is enclosed in `"` because of file name (or its path) contains a space or one of these characters ``&()[]{}^=;!'+,`~``. This option is not necessary on using just `myfile.txt` and not `"myfile.txt"` as I wrote in my comment above. – Mofi Feb 15 '20 at 11:12