# Passing parameter to java main function

**URL:** https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091
**Category:** Old Forum Archive
**Created:** [August 13, 2014, 12:27am UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091 "2014-08-13T00:27:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ajay\_Pande](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Ajay\_Pande](https://discuss.gradle.org/u/Ajay_Pande)
#### Post date: [August 13, 2014, 12:27am UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/1 "2014-08-13T00:27:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)
#### Post date: [August 13, 2014, 12:58am UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/2 "2014-08-13T00:58:00Z")

</div>

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

```gradle
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.

---

<div class="post-metadata">

### Author: ![Ajay\_Pande](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Ajay\_Pande](https://discuss.gradle.org/u/Ajay_Pande)
#### Post date: [August 13, 2014, 4:40am UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/3 "2014-08-13T04:40:00Z")

</div>

hi,

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!

---

<div class="post-metadata">

### Author: ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)
#### Post date: [August 13, 2014, 11:18am UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/4 "2014-08-13T11:18:00Z")

</div>

```gradle
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.

---

<div class="post-metadata">

### Author: ![Ajay\_Pande](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@Ajay\_Pande](https://discuss.gradle.org/u/Ajay_Pande)
#### Post date: [August 13, 2014, 6:12pm UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/5 "2014-08-13T18:12:00Z")

</div>

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.

---

<div class="post-metadata">

### Author: ![ghhale](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ghhale/32/26_2.png) [@ghhale](https://discuss.gradle.org/u/ghhale)
#### Post date: [August 13, 2014, 6:14pm UTC](https://discuss.gradle.org/t/passing-parameter-to-java-main-function/2091/6 "2014-08-13T18:14:00Z")

</div>

```gradle
if (project.hasProperty('poolVersion1') {
  args = ...
}

```
