Gradle Execute external program

Dear my friends,

I faced a problem when executing external program with Gradle. Hope you will help me to solve this. Thank you all :slight_smile:
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

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?

1 Like

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

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()}”

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.