Gradle shell script corrupts JAVA_HOME environment variable on Windows/Cygwin

Hi all,

I tried to use the JAVA_HOME environment variable inside a build script to exec the xjc tool from the JDK. This worked fine from the Windows command shell, but failed when called from the Cygwin shell. Apparently, the “gradle” shell script (from the Gradle bin folder) modifies the JAVA_HOME environment variable.

The build file (simplified):

task printXjcVersion << {

exec {

commandLine(

System.getenv(“JAVA_HOME”) + “/bin/xjc.exe”,

“-version”

)

ignoreExitValue = true

}

}

The output from the Windows shell:

C:>gradle -q printXjcVersion

xjc 2.2.4-2

The output from the Cygwin shell:

$ gradle -q printXjcVersion

FAILURE: Build failed with an exception.

  • What went wrong:

Execution failed for task ‘:printXjcVersion’.

A problem occurred starting process ‘command ‘/cygdrive/c/Program Files/Java/JDK1.7.0_11/bin/xjc.exe’’

  • Try:

Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

So the problem is that the cygwin-ified version of JAVA_HOME is useless outside of the starter script. I devised a simple fix for the gradle shell script. The idea is to modify the JAVACMD local variable instead of JAVA_HOME. This works for me. I’m not a shell expert, though.

The output from the Cygwin shell with my fix:

$ gradle -q printXjcVersion

xjc 2.2.4-2

I’m using Gradle 1.4 on Windows. I attached my simple patch.

Regards Jörg

--- C:/Projects/Programs/Gradle-1.4/bin/gradle_orig Mon Jan 28 04:47:48 2013
+++ C:/Projects/Programs/Gradle-1.4/bin/gradle_fixed Wed Feb 06 14:40:29 2013
@@ -42,11 +42,6 @@
     ;;
 esac
   -# For Cygwin, ensure paths are in UNIX format before anything is touched.
-if $cygwin ; then
-
  [ -n "$JAVA_HOME" ] && JAVA_HOME='cygpath --unix "$JAVA_HOME"'
-fi
-
 # Attempt to set APP_HOME
 # Resolve links: $0 may be a link
 PRG="$0"
@@ -114,6 +109,7 @@
 if $cygwin ; then
     APP_HOME='cygpath --path --mixed "$APP_HOME"'
     CLASSPATH='cygpath --path --mixed "$CLASSPATH"'
+
  JAVACMD='cygpath --unix "$JAVACMD"'
        # We build the pattern for arguments to be converted via cygpath
     ROOTDIRSRAW='find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null'

Thanks Joerg, would you be willing to submit a pull request for this?

The file in question is here: https://github.com/gradle/gradle/blob/master/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt

Hi Luke,

I’m doing this from work and don’t have Git nor direct internet access. I suggest you take the patch and use it in whatever way you like.

Regards Jörg

P.S. I’m absolutely planning to learn Git asap :wink: because Linus calls me “ugly and stupid” for using Subversion

Ok, thanks for the patch (GRADLE-2673).