Batch file to run jar file with parameters

How to run batch file and pass parameters to jar file?

this does not work

mybat.bat

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

current bat file

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

java only sees -file

+7
source share
4 answers

I just tried a small java program that only resets 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 got the following result

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

For the same command line as you ... the equal sign '=' desapeared. Now, if you dragged a batch file onto this:

 java -jar test.jar %* 

you get another result (which may be what you expected - not clear)

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

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

I hope this helps, but I recommend that you take a look at your code and add debugging instructions to understand where you are β€œlosing” part of the input.

+18
source

1) You can try using

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

Variables will be set as a property of the Java System, but not as parameters in the main method.

2) Try to put arguments without '='

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

0
source

In my case, I use the following bat file:

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

In this particular case, in java code, I can get the "enable" parameter as follows:

 Boolean.getBoolean("enable") 
0
source

An example of opening the 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
source

All Articles