Passing parameter to java main function

How to pass java main function args using gradle run ? I am trying ./gradlew run poolVersion1=294 but getting error poolVersion1 should get passed to my main function args array.

You have to define the args in the JavaExec task configuration.

run {
  args = [ 'poolVersion1=294' ]

If you want those to come from the command line, you can set a project property with “-P” and then set the args based off the value of the property.


Thank you so much for your reply its really helpful. But I still did not got how can we send value using -P and then set the args?

do you have any example for it?

Thanks in advance!

run {
  args = [ "poolVersion1=${poolVersion1}" ]

Then your command line would look like:

gradle run -PpoolVersion1=294

“-P” sets a property on the project object which you can then access from the script.

Yes that work great but what if I don’t pass poolVersion1 in -P it will throw error, I actually dont want it as mandatory.

if (project.hasProperty('poolVersion1') {
  args = ...