# Return value from java process in gradle task

**URL:** <https://discuss.gradle.org/t/return-value-from-java-process-in-gradle-task/3611>\
**Category:** Old Forum Archive\
**Created:** [June 28, 2013, 1:21pm UTC](https://discuss.gradle.org/t/return-value-from-java-process-in-gradle-task/3611 "2013-06-28T13:21:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![u1234](https://avatars.discourse-cdn.com/v4/letter/u/2acd7d/32.png) [@u1234](https://discuss.gradle.org/u/u1234)\
**Post date:** [June 28, 2013, 1:21pm UTC](https://discuss.gradle.org/t/return-value-from-java-process-in-gradle-task/3611/1 "2013-06-28T13:21:00Z")

</div>

Is it possible to return a value from a class executed by the gradle javaExec task? I have:

```gradle
task myTask(dependsOn: classes) << {
    javaexec {
      classpath = sourceSets.test.runtimeClasspath
      main = 'com.samples.MyClass'
      args=["test"]
    }
         // Continue using the value from the MyClass there!
        }

```

And where the MyClass is:

```gradle
public class MyClass {
  public static void main(String[] args) {
    List<String> result = new ArrayList<String>();
    result.add("v0");
    result.add("v1");
    result.add("v2");
          String buildString = "";
    for (String string : result) {
      buildString+=string;
    }
    System.out.println(buildString);
      }

```

---

<div class="post-metadata">

**Author:** ![ajoberstar](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/ajoberstar/32/127_2.png) [@ajoberstar](https://discuss.gradle.org/u/ajoberstar)\
**Post date:** [June 30, 2013, 3:31pm UTC](https://discuss.gradle.org/t/return-value-from-java-process-in-gradle-task/3611/2 "2013-06-30T15:31:00Z")

</div>

Direct answer to your question… You’ll need access to what’s written to the output stream. I think something like this would work:

```gradle
task myTask(dependsOn: classes) << {
  def os = new ByteArrayOutputStream()
      javaexec {
    classpath = sourceSets.test.runtimeClasspath
    main = 'com.samples.MyClass'
    args=["test"]
    standardOutput = os
  }
  def buildString = os.toString()
  // Continue using the value from the MyClass there!
}

```

On the other hand, you could just directly use ‘MyClass’ as part of the build. That would avoid the extra overhead of a separate task. If you put that class into a plugin JAR, or into ‘buildSrc’, or even just declare it directly in the ‘build.gradle’, it would just be on the build’s classpath.
