Thursday, January 13, 2011

Execute a Java application on Windows using a batch file with command line arguments

It is tricky to write batch file aimed to execute a java application on windows if you want to process command line arguments coming from the Windows OS. Windows will report path with \ and add some " automatically when draging a file or a path on the batch file itself. While when reunning from the command line you can past a windows path using the clipboard.

On the other end we all know that java is much more happy with / inside file path.

So assuming %1 is the argument you will receive from the OS, you need to replace to / and then make sure you remove the quote since you are adding the " in the batch file.

REM Replace - Replace a substring using string substitution
echo %1
set str=%1
set str=%str:\=/%
REM Trim Quotes - Remove surrounding quotes via FOR command
for /f "useback tokens=*" %%a in ('%str%') do set str=%%~a

java com.mycompany.MYAPP --arg1 --arg2 --input-file="%str%" --output-file="%str%.out"

more tips at

No comments: