passing parameter to a jar file which is called in a bat file?

前端 未结 3 819
花落未央
花落未央 2021-01-20 12:47

I have created a jar which needs to be called in a bat file. I need to pass all the command line arguments recieved by bat file to the jar. Can anyone please help me out. I

相关标签:
3条回答
  • 2021-01-20 13:20

    Inside you bat file you will have java command
    just use java -jar helloworld.jar firstParam secondParam and
    I believe you can also use because that how we pass params to Maven and ANT etc

    mybatchFile.bat -DfirstParam -DsecondParam
    
    0 讨论(0)
  • 2021-01-20 13:22

    The parameters that you pass to your batch file can be accessed via

    %1 %2 %3 ...
    

    So if you call your batch like

    C:>application.bat param1 param2 param3
    

    then your java call inside the batch file should look like:

    @echo off
    java -cp app.jar com.example.Main %1 %2 %3
    
    0 讨论(0)
  • 2021-01-20 13:33

    If you don't know how many parameters the users could pass to the batch file (if any) you need to get the no. of arguments whatever the user has passed, simply add the following snippet to your main method at the start

    for (String s : args) {
        // Iterate through String array in Java (args list)
        System.out.println(s);
    }
    

    You can store all the arguments in an Arraylist to use it and iterate over it later

    0 讨论(0)
提交回复
热议问题