开发者

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 o开发者_Go百科nly -file


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.


1) You could try to use

"-Dfile=C:\trace_small.log -Dstr=Storing"

The variables would be set as Java System Property, but not as Parameters into a main-method.

2) Try to put the arguments without '='

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


In my case I use the following bat-file:

@echo off
PATH_TO_JRE\bin\java.exe -jar -Denable=true your_file.jar

At this particular case then in java code I can get the param "enable" like this:

Boolean.getBoolean("enable") 


For Example Opening Street Map Editor in Windows 10 x64

> cd "C:\Program Files\Java\jre1.8.0_161\bin\"

> javaw.exe -Xmx2048m -jar "C:\Program Files (x86)\JOSM\josm-tested.jar"
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜