# Gradle Execute external program

**URL:** <https://discuss.gradle.org/t/gradle-execute-external-program/19177>\
**Category:** Help/Discuss\
**Created:** [August 25, 2016, 4:08am UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177 "2016-08-25T04:08:48Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhatuan84](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nhatuan84](https://discuss.gradle.org/u/nhatuan84)\
**Post date:** [August 25, 2016, 4:08am UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177/1 "2016-08-25T04:08:48Z")

</div>

Dear my friends,

I faced a problem when executing external program with Gradle. Hope you will help me to solve this. Thank you all 🙂  
PS: I used Gradle for an embedded project (C language not a Java project)  
I will describe my problem:

1. My development environment is Ubuntu.
2. I used an external program for Unit test (It is called Cantata), From Linux I will type a command “./cantpp” to invoke its GUI (eclipse based)
3. After finishing working on GUI I will integrate last result to Gradle so that Gradle will invoke a command line to generate Test Report.  
The command line like below:

$CANTATA\_INSTALL\_DIR/cantpp -application com.ipl.products.eclipse.cantpp.testscript.TestSummaryGenerator -noSplash -data Cantata/UnitTest project=Cantata/UnitTest/abcdef 1 (with this command like on shell the GUI will not be invoked by option ‘-noSplash’, and I get the report successfully)

And Gradle code for that command line is:

def commandArray = new String[3]  
commandArray[0] = System.getenv(“CANTATA\_INSTALL\_DIR”) + "/cantpp"  
commandArray[1] = "application"  
commandArray[2] = "com.ipl.products.eclipse.cantpp.testscript.TestSummaryGenerator -noSplash -data Cantata/UnitTest project=Cantata/UnitTest/gDMPxy 1"  
def process = new ProcessBuilder(commandArray)  
.directory(new File(curPath))  
.redirectErrorStream(true)  
.start()  
process.inputStream.eachLine {println it}  
process.waitFor();

But unfortunately, when Gradle is running, the GUI is invoked so the command line is not executed like I expect.

So my question is “Are there any ways to execute the command line in Gradle which the GUI is not invoked?”

Thanks and Best regards  
Tuan

---

<div class="post-metadata">

**Author:** ![st\_oehme](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/st_oehme/32/5249_2.png) [@st\_oehme](https://discuss.gradle.org/u/st_oehme)\
**Post date:** [August 25, 2016, 5:23pm UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177/2 "2016-08-25T17:23:41Z")

</div>

> [@nhatuan84](#):
>
> commandArray[2] = “com.ipl.products.eclipse.cantpp.testscript.TestSummaryGenerator -noSplash -data Cantata/UnitTest project=Cantata/UnitTest/gDMPxy 1”

This line is the problem. When building a process, every argument needs to go in a separate element of the array.

Also, why are you using ProcessBuilder instead of Gradle’s much more covenient [Exec](https://docs.gradle.org/current/dsl/org.gradle.api.tasks.Exec.html) task or [exec](https://docs.gradle.org/current/dsl/org.gradle.api.Project.html#org.gradle.api.Project:exec(org.gradle.api.Action)) method?

---

<div class="post-metadata">

**Author:** ![nhatuan84](https://avatars.discourse-cdn.com/v4/letter/n/b5a626/32.png) [@nhatuan84](https://discuss.gradle.org/u/nhatuan84)\
**Post date:** [August 26, 2016, 6:29am UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177/3 "2016-08-26T06:29:34Z")

</div>

> [@st\_oehme](#):
>
> This line is the problem. When building a process, every argument needs to go in a separate element of the array.
> 
> Also, why are you using ProcessBuilder instead of Gradle’s much more covenient Exec task or exec method?

Dear friend,  
Tks for your answer  
I tried  
def commandArray = new String[4]  
commandArray[0] = System.getenv(“CANTATA\_INSTALL\_DIR”) + “/cantpp”  
commandArray[1] = “application com.ipl.products.eclipse.cantpp.testscript.TestSummaryGenerator”  
commandArray[2] = “noSplash”  
commandArray[3] = “data Cantata/UnitTest project=Cantata/UnitTest/gDMPxy 1”  
def process = new ProcessBuilder(commandArray)  
.directory(new File(curPath))  
.redirectErrorStream(true)  
.start()  
process.inputStream.eachLine {println it}  
process.waitFor();  
But it not work.

I also tried Exec and exec but it also not work

Best regards  
Tuan

---

<div class="post-metadata">

**Author:** ![HariSharma](https://avatars.discourse-cdn.com/v4/letter/h/eb9ed0/32.png) [@HariSharma](https://discuss.gradle.org/u/HariSharma)\
**Post date:** [October 16, 2024, 12:48pm UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177/4 "2024-10-16T12:48:14Z")

</div>

Is it resolved or not, I tried the below code and it was working.  
def cmd = commandLine  
def process1 = cmd.execute()  
process1.waitFor()  
println “Output: ${process1.text.trim()}”

---

<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:** [October 16, 2024, 5:43pm UTC](https://discuss.gradle.org/t/gradle-execute-external-program/19177/5 "2024-10-16T17:43:42Z")

</div>

Your question is not really related to the thread.  
Of course you can use Java’s `ProcessBuilder` as OP does,  
or Groovy’s `.execute()` like you do  
to execute some external process, you just shouldn’t,  
but instead as Stefan said use a task of type `Exec` or an `exec { .... }` block.

But besides of that, the question here said not, that it is not possible to execute an external process,  
but that the external process shows a GUI which OP did not want, most probably because he did not supply the arguments properly as in his first try he supplied one argument with value `com.ipl.products.eclipse.cantpp.testscript.TestSummaryGenerator -noSplash -data Cantata/UnitTest project=Cantata/UnitTest/gDMPxy 1` and in his second try an argument with value `noSplash`.

Besides that the question has exactly zero to do with Gradle, it should most probably be an argument with value `-noSplash` instead, but that depends on that command that is called and how it needs to be called and not at all a Gradle topic.
