1

I start shell scripts with java Process.exec (for reasons), and they always return 0 exit value, regardless of exit value of script itself (which I can check by running said script in OS shell. For additional info, those scripts run gradle tasks with gradle-node-plugin. Any pointers for this issue?

Overall, setup looks as follows:

Software

  • Linux desktop 4.4.0-130-generic #156~14.04.1-Ubuntu SMP Thu Jun 14 13:51:47 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux`

  • java version "1.8.0_171" Java(TM) SE Runtime Environment (build 1.8.0_171-b11) Java HotSpot(TM) 64-Bit Server VM (build 25.171-b11, mixed mode)`

Shell script is executed with

Process process = Runtime.getRuntime().exec(new String[]{"sh", "-c", testScript});

Return value is obtained with test.exitValue(), and appears to be always 0.

Shell script calls gradle ./gradlew project:clean project:test.

Gradle scripts relies on gradle-node-plugin

plugins {
    id "com.moowork.node" version "1.2.0"
}

task test(type: NodeTask) {
    script = file('./node_modules/mocha/bin/mocha')
    args = ['test/sometest.js']
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
m.ostroverkhov
  • 1,910
  • 1
  • 15
  • 17
  • 1
    Please post the code please, else we might as well guess what Java thinks – Lino Jul 26 '18 at 19:07
  • @Lino I added some code snippets – m.ostroverkhov Jul 26 '18 at 19:53
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) See also [When Runtime.exec() won't](http://www.javaworld.com/article/2071275/core-java/when-runtime-exec---won-t.html) for many good tips on creating and handling a process correctly. Then ignore it refers to `exec` and use a `ProcessBuilder` to create the process. Also break a `String arg` into `String[] args` to account for things like paths containing space characters. – Andrew Thompson Jul 26 '18 at 22:59
  • 1
    Tried to set a value to environment variable `GRADLE_EXIT_CONSOLE` prior to calling [gradlew](https://gitlab.leibniz-zmt.de/ecomod/zmt-build/blob/2.1.0/gradlew.bat) ? – Little Santi Jul 26 '18 at 23:22
  • @LittleSanti That solves my problem pretty nicely - wow! Interesting fact, there is not much info on the internet, but it is present in gradle's sources (and in every gradlew file!). Would like to know how did you come up with that knowledge, but in the meantime please post your comment as answer so I can accept It. – m.ostroverkhov Jul 27 '18 at 06:32
  • Fine! I've posted my answer. Well, I just traced the problem starting by the gradle script, and that is how I saw the way it handles the exit codes. – Little Santi Jul 27 '18 at 10:24

1 Answers1

2

Gradle scripts prevent the possibility to return the exit code to the same shell, or even ending the calling shell. This is done through the use of environment variable GRADLE_EXIT_CONSOLE (an arbitrary, non-empty value is enough):

  • If set: The script ends the shell and returns the exit code.
  • If not set: The script returns the exit code to the calling shell, allowing it to continue processing commands.

If you are calling gradle through Runtime.exec, it creates a new shell, so you need to set that variable prior to calling the script, so you get the exit code after the shell is ended.

Little Santi
  • 8,563
  • 2
  • 18
  • 46