title: Gradle uses wrong JAVA_HOME when running cordova android build
body: <p><strong>Update:</strong>
  I tried removing every trace of java openjdk, installing Oracle Java 8, and setting up my environment variables again but still no bueno. So after I did <code>source ~/.bashrc</code> I closed all terminals, logged out, and viola, all fixed. What a hassle though.</p>
  
  <p><strong>OP:</strong>
  I came across this <a href="https://stackoverflow.com/questions/22307516/gradle-finds-wrong-java-home-even-though-its-correctly-set">SO question</a> that seems to deal with the same issue I am having. Unfortunately, I didn't have the line <code>export JAVA_HOME=/usr/lib/jvm/default-java</code> in my <code>usr/bin/gradle</code> file.</p>
  
  <p>My error says <code>JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre</code> </p>
  
  <p>But <code>echo $JAVA_HOME</code> returns <code>/usr/lib/jvm/java-8-openjdk-amd64/jre</code></p>
  
  <p>I tried creating a symlink between the real JAVA_HOME and the one gradle keeps trying to use but the one gradle is trying to use isn't real so I get the error <code>ln: failed to create symbolic link '/usr/lib/jvm/java-7-openjdk-amd64/jre': No such file or directory
  </code></p>
  
  <p>I tried digging around the gradlew file and found what I think may be the offending code but I'm not 100% sure how it works and any time I edit the file and then execute <code>cordova run android</code> all my changes are wiped away. </p>
  
  <p>This is the bit of code I found in gradlew:</p>
  
  <pre><code># Determine the Java command to use to start the JVM.
  if [ -n "$JAVA_HOME" ] ; then
      if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
          # IBM's JDK on AIX uses strange locations for the executables
          JAVACMD="$JAVA_HOME/jre/sh/java"
      else
          JAVACMD="$JAVA_HOME/bin/java"
      fi
      if [ ! -x "$JAVACMD" ] ; then
          die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
  
  Please set the JAVA_HOME variable in your environment to match the
  location of your Java installation."
      fi
  else
      JAVACMD="java"
      which java &gt;/dev/null 2&gt;&amp;1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
  
  Please set the JAVA_HOME variable in your environment to match the
  location of your Java installation."
  fi
  </code></pre>
  
  <p>My full error report:</p>
  
  <pre><code>Running command: /home/grant/Development/Projects/ACA/hooks/after_prepare/icons_and_splashscreens.js /home/grant/Development/Projects/ACA
  Running command: /home/grant/Development/Projects/ACA/hooks/after_prepare/update_platform_config.js /home/grant/Development/Projects/ACA
  ANDROID_HOME=/home/grant/Development/Android
  JAVA_HOME=/usr/lib/jvm/java-7-openjdk-amd64/jre
  No target specified, deploying to device '05157df5a3d39408'.
  
  ERROR: JAVA_HOME is set to an invalid directory: /usr/lib/jvm/java-7-openjdk-amd64/jre
  
  Please set the JAVA_HOME variable in your environment to match the
  location of your Java installation.
  
  ERROR running one or more of the platforms: Error code 1 for command: /home/grant/Development/Projects/ACA/platforms/android/gradlew with args: cdvBuildDebug,-b,/home/grant/Development/Projects/ACA/platforms/android/build.gradle,-PcdvBuildArch=arm,-Dorg.gradle.daemon=true,-Pandroid.useDeprecatedNdk=true
  You may not have the required environment or OS to run this project
  </code></pre>