# ProcessBuilder or Exec for calling external compiler from WorkAction?

**URL:** <https://discuss.gradle.org/t/processbuilder-or-exec-for-calling-external-compiler-from-workaction/49221>\
**Category:** Help/Discuss\
**Created:** [August 22, 2024, 8:02pm UTC](https://discuss.gradle.org/t/processbuilder-or-exec-for-calling-external-compiler-from-workaction/49221 "2024-08-22T20:02:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Martin\_d\_Anjou](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/martin_d_anjou/32/5248_2.png) [@Martin\_d\_Anjou](https://discuss.gradle.org/u/Martin_d_Anjou)\
**Post date:** [August 22, 2024, 8:02pm UTC](https://discuss.gradle.org/t/processbuilder-or-exec-for-calling-external-compiler-from-workaction/49221/1 "2024-08-22T20:02:13Z")

</div>

In writing a plugin for a custom compiler (linux), inside a [WorkAction (Gradle API 8.10)](https://docs.gradle.org/current/javadoc/org/gradle/workers/WorkAction.html), should I use a [ProcessBuilder (Java SE 17 & JDK 17)](https://docs.oracle.com/en%2Fjava%2Fjavase%2F17%2Fdocs%2Fapi%2F%2F/java.base/java/lang/ProcessBuilder.html) or should I use an instance of [Exec (Gradle API 8.10)](https://docs.gradle.org/current/javadoc/org/gradle/api/tasks/Exec.html)?

I will need to consume stdout and stderr so the process does not block, and I need to get the exit status, as well as parse the contents of stdout and stderr. Multiple invocations should also work in parallel, and everything must halt itself if an error occurs (no point in keeping the other parallel invocation going if one fails). I looked at how Gradle supports Cpp but the code is too hard to follow for me to draw my own conclusion. Thanks in advance for any advice.

---

<div class="post-metadata">

**Author:** ![Vampire](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/vampire/32/9082_2.png) [@Vampire](https://discuss.gradle.org/u/Vampire)\
**Post date:** [August 24, 2024, 2:02am UTC](https://discuss.gradle.org/t/processbuilder-or-exec-for-calling-external-compiler-from-workaction/49221/2 "2024-08-24T02:02:47Z")

</div>

I don’t think there is really a good way to abort already scheduled work if one of the work-items failed.

An instance of `Exec` you cannot use, `Exec` is a task.  
But if you meant injecting `ExecOperations` and using `exec { ... }` on it, then yes, I’d prefer that over a plain `ProcessBuilder`.  
You get an `ExecResult` from which you can get the exit code if you need more than “fail if != 0”.  
You can set the standard streams to anything you need to provide and consume the content as necessary, …

The parallel execution you already have by using the worker api, that is one of its major benefits.
