20

New to Android Studio + Gradle.

I'm trying to run my app from the command line using:

gradlew installDebug

The app gets installed on my device only when I execute that command when Studio just got opened and do that initial syncing/building thing.

Whenever I modified my code, and try to run that command again, the build fails and shows this error:

Execution failed for task ':app:compileDebugJavaWithJavac'
> Could not find tools.jar

How do I solve this problem? Thanks in advance.

zepolyerf
  • 1,098
  • 3
  • 16
  • 35

9 Answers9

25

Setting the JAVA_HOME variable to C:\Program Files\Java\jdkX.XX worked for me.

It was previously set to the JRE directory for some reason.

zepolyerf
  • 1,098
  • 3
  • 16
  • 35
  • 7
    this is the correct fix. Detailed how-to on Windows 10: **1)** go to Search bar and type in "environment" and click on "Edit the system environment variables". **2)** click on "Environment Variables" button **3)** under System Variables heading, click "new". Enter the name as JAVA_HOME and look up the path to the jdk like Jeffrey said. Click OK and OK again. **4)** close your DOS window and open a new one. Now gradlew will work fine – Someone Somewhere Feb 24 '16 at 12:19
  • Note: in windows you check the variable by typing in `echo %JAVA_HOME%` on a DOS window. It should show you the C:\Program Files\Java\... path. If it doesn't, then you didn't successfully create the variable, or you didn't exit the DOS window after you created the system variable. – Someone Somewhere Feb 24 '16 at 12:25
  • For me what worked was adding the correct %`JAVA_HOME`% + `/lib` to the `PATH` variable. From[this answer](http://stackoverflow.com/a/35623142/1219280) – Veverke Feb 15 '17 at 13:37
8

Are you on Linux? What does echo $JAVA_HOME outputs?

I had a similar issue when running react-native run-android. After setting $JAVA_HOME (which hadn't existed) to a JDK folder, the problem was solved. In my case, I added the following line to the end of .bashrc file:

export JAVA_HOME="path/to/a/folder/with/JDK/jdk1.8.0_60"

and restarted the terminal.

Jared Rummler
  • 37,824
  • 19
  • 133
  • 148
Nil
  • 174
  • 1
  • 3
  • 14
  • I'm not OP but I have a similar error (same task, different error) and I'm on Ubuntu. I didn't had `JAVA_HOME` so I added it to `/usr/lib/jvm/java-8-oracle` and same error. – Calvein Nov 02 '15 at 01:44
  • 1
    I just "fixed" the problem, my react-native project was called `native` and you can't do that: https://github.com/facebook/react-native/issues/3437#issuecomment-152888456 – Calvein Nov 02 '15 at 01:57
  • Restarting terminal is also an important part +1 – Nick Feb 01 '16 at 16:01
  • 3
    NOTE: this is stupid but make sure you have the JDK actually installed (not just the JRE) in Ubuntu you run: `sudo apt-get install openjdk-7-jdk`. It will give you the same error. – JustGage Apr 20 '16 at 03:53
6

I deleted .gradle file from project root directory and rebuilt the app and then everything was working fine.

shivtej
  • 633
  • 9
  • 18
3

For anyone facing this issue after updating to AndroidStudio-2.0

I previously had AndroidStudio-1.3 and jdk-1.7.X_XX. When I updated to AndroidStudio-2.0, things worked fine until I rebooted the computer. After reboot, I encountered this error and none of the above answers worked for me. Here's what I did:

  1. Copied the project into a new folder
  2. Upgraded my jdk version from 1.7.X_XX to 1.8.X_XX
  3. Changed the JDK's path in the project structure to the new path:

    C:\Program Files\Java\jdk1.8.X_XX

  4. Gradle sync

And things worked.

kds23
  • 296
  • 3
  • 17
2

ALL above answers are correct. I just wanna mention that if you are using MAC, and the full path of JDK path should be

"/Library/Java/JavaVirtualMachines/jdk1.8.0_65.jdk/Contents/Home"

Do not miss /Contents/Home

Yuchao Zhou
  • 1,062
  • 14
  • 19
1

The previous answers did not work for me. This is what worked: (Ubuntu, but should work for others as well)

  1. Go to your project folder:/.../projectlocation

  2. Then to android folder: /.../projectlocation/android

  3. Find gradle.properties file and add this line anywhere in the file: org.gradle.java.home=/usr/local/jdk1.8.0_91

Notice that there are no quotes and the right side of the example is my JDK folder.

Tanel
  • 1,080
  • 9
  • 16
0

I faced the same issue.I was looking for several hours on stack overflow and tried all of the way they said but was not able to solve the problem.Then I found that problem was in my project folder name.I just rename the folder name and everything worked fine.

Md Nakibul Hassan
  • 2,716
  • 1
  • 15
  • 19
0

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home/"

i'am using zshrc i added up line. after that carefully echo $JAVA_HOME in terminal You should see /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home after that build will be done.

Şafak Çıplak
  • 889
  • 6
  • 12
0

Execution Failed for task :app:compileDebugJavaWithJavac in Android Studio

-> after 7 hours i found SOLUTION of this issue in my project level "build.gradle" file.

-> Simple to Remove only 2 things from your "build.gradle" file.

        buildscript { 
          maven { url 'https://jitpack.io' }
        }
        
        allprojects{
            mavenCentral{ url "http://jcenter.bintray.com/" }
            maven { url "https://jitpack.io" }
        }

-> and update the version of gradle just like.

        dependencies {
                classpath 'com.android.tools.build:gradle:4.2.2'
        }
        

-> done.