[GRADLE-2673] Gradle shell script corrupts JAVA_HOME environment variable on Windows/Cygwin Created: 07/Feb/13  Updated: 30/Jul/15  Resolved: 30/Jul/15

Status: Resolved
Project: Gradle
Affects Version/s: None
Fix Version/s: 2.7-rc-1

Type: Bug
Reporter: Gradle Forums Assignee: Unassigned
Resolution: Fixed Votes: 1


 Description   

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



 Comments   
Comment by Gradle Forums [ 07/Feb/13 ]

— 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

    1. 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
    -
  1. Attempt to set APP_HOME
  2. 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"`
  1. We build the pattern for arguments to be converted via cygpath
    ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Comment by Gradle Forums [ 07/Feb/13 ]

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

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

Comment by Gradle Forums [ 07/Feb/13 ]

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 because Linus calls me "ugly and stupid" for using Subversion

Generated at Wed Jun 30 12:28:20 CDT 2021 using Jira 8.4.2#804003-sha1:d21414fc212e3af190e92c2d2ac41299b89402cf.