Wednesday, September 06, 2006

Check required arguments for batch files

Sometimes you need to create a batch file (*.BAT, *.CMD) to run a Java application, but your application need to pass some arguments from outside. It will be better if you can provide the arguments from command prompt instead using hard code inside the batch file. At that time, you should provide some basic logical to check the number of arguments you pass into the batch file.

Of course, it cannot replace logical to check required arguments inside your Java application ('cause user can run your Java application without the batch file). But it can provide a more flexible way to run Java application.

And here is your solution:

HelloKitty class requires to pass 2 arguments, they're presented by %1 and %2 and assume that you store this code segment into hello.bat

@echo off
if "%2"=="" goto error

java HelloKitty %1 %2
goto end

:error
echo Usage: hello arg1 arg2

:end

No comments: