Search code examples
javabatch-filejar

batch file to run jar file with parameters


How can I run a batch file and pass parameters to jar file?

this doesn't work

mybat.bat

java -jar log_parser.jar %1 %2 %3 %4

running bat file

C:\>log_parser.bat -file=C:\\trace_small.log -str=Storing

java sees only -file


Solution

  • I just tried with a small java program that only dumps the arguments to the screen:

    public static void main(String[] args)
    {
        for(String s : args)
        {
            System.out.println(s);
        }
    }   
    

    and the following batch file :

    java -jar test.jar %1 %2 %3 %4
    

    and I ended up with the following result

    -file
    C:\\trace_small.log
    -str
    Storing
    

    For the same command line as you... the equal sign '=' desapeared. Now if you trun the batch file to this :

    java -jar test.jar %*
    

    you will get yet another result (which might be what you expected - not clear)

    -file=C:\\trace_small.log
    -str=Storing
    

    The advantage on this %* syntax, is that it is more extensible by accepting any number of arguments.

    Hope this helps, but I recommend you to have a look at your code to and add some debug statement to understand where you are "lossing" some part of the input.