123

Even though I manually configured JDK project structure file/Project structure it still shows this error FAILURE: Build failed with an exception.

`What went wrong: Execution failed for task ':sample:compileReleaseJavaWithJavac'.

Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_151 contains a valid JDK installation.`

I'm confused why it is still looking for C:\Program Files\Java\jre1.8.0_151 instead of JDK

Viswesvar Sekar
  • 2,743
  • 3
  • 11
  • 7
  • 4
    What is the current value of the `JAVA_HOME` environment variable? And is the JRE higher in your `PATH`? Why even install a JRE (the JDK includes one)? – Elliott Frisch Nov 14 '17 at 16:50
  • I have jdk1.7.0_79, jdk1.8.0_144, jre1.8.0_151 inside c:/windows/program Files/ java – Viswesvar Sekar Nov 14 '17 at 16:58
  • 1
    That does not answer any one of my three distinct questions. Also, `c:/windows/program Files/`? Are you sure? – Elliott Frisch Nov 14 '17 at 17:00
  • https://i.stack.imgur.com/VxQ2W.jpg, and for the second one the JRE is same as mentioned in the terminal C:\Program Files\Java is my directory where JDK remains, by default in Android studio it was this directory configured in my studio C:\Program Files\Android\Android Studio\jre – Viswesvar Sekar Nov 14 '17 at 17:09
  • `echo %PATH%` and `echo %JAVA_HOME%`. – Elliott Frisch Nov 14 '17 at 17:10
  • For JAVA_HOME its C:\Users\viswesvar>echo %JAVA_HOME% %JAVA_HOME% For echo %JAVA_HOME% C:\ProgramData\Oracle\Java\javapath;C:\Program Files – Viswesvar Sekar Nov 14 '17 at 17:28
  • 1
    I fixed it by adding JAVA_HOME to C:\Program Files\Android\Android Studio\jre – Pedram Vdl Jan 17 '19 at 21:17
  • For MacOS users, check out my answer on another thread: [Execution failed for task ':app:compileDebugJavaWithJavac'. > Could not find tools.jar](https://stackoverflow.com/a/65560333/988521) – Natan Lotério Jan 04 '21 at 09:30
  • My problem was that I had to use JDK instead of JRE. – tomasantunes Sep 22 '22 at 09:42

27 Answers27

123

At last, here I found the solution.

I added jdk path org.gradle.java.home=C\:\\Program Files\\Java\\jdk1.8.0_144 to gradle.properties file and did a rebuild. It works now.

Kushal
  • 8,100
  • 9
  • 63
  • 82
Viswesvar Sekar
  • 2,743
  • 3
  • 11
  • 7
108
ERROR: Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_121 contains a valid JDK installation

Copy tools.jar from C:\Program Files\Java\jdk1.8.0_121\lib to C:\Program Files\Java\jre1.8\lib.

It's resolved the problem now.

Grant Miller
  • 27,532
  • 16
  • 147
  • 165
Anup Singh
  • 1,115
  • 1
  • 7
  • 3
  • 3
    I just updated my Android Studio from version 3.2 to version 3.3 and then faced this problem. After hours of googling your solution finally helped me. Thanks! – Alexei Artsimovich Jan 17 '19 at 15:11
  • After trying so many solutions (changing editors, installing new plugins ...), this one solved my problem, thanks. – Noblesse Apr 26 '19 at 10:38
  • 2
    This solution works fine for me. Stunned by copy/paste of tools.jar file manually. Anyway great solution. – dipakbari4 Aug 03 '19 at 18:21
  • This solution worked for me too, thank you But this is really weird This really needs to be done automatically – masoudmanson Sep 21 '19 at 05:42
  • 1
    Damn JDK. Was not able to add it to the system variables as well. But just copying the tools.jar worked perfectly. Thanks. – kwoxer Apr 25 '20 at 05:33
39

Just had this happen to me.

Apparently Java's automatic updater installed and configured a new version of the JRE for me, while leaving the old JDK intact. So even though I did have a JDK, it didn't match the currently "active" JRE, which was causing the error.

Download a matching version of the JDK to the JRE you currently have installed, (In OP's case 151) That should do the trick.

Marco Aurélio
  • 2,463
  • 1
  • 21
  • 21
23

I had the same problem and fixed it like this:

  1. Install the JDK from the Oracle website with the same version number as the JRE if you didn't already.
  2. Furthermore add JAVA_HOME to the environment variables of Windows.
  3. Restart your terminal or development environment to load the new JAVA_HOME value.

JAVE_HOME in System Environment Variables

Ton Snoei
  • 2,637
  • 22
  • 23
4

This happens when JDK and JRE have different versions installed on your system. Update the JDK with the matching version of JRE. Also verify that System variable path has bin value from same JDK version.

Sunny Prakash
  • 772
  • 1
  • 8
  • 13
4

The problem is your gradle build is not finding the JAVA_HOME path or JDK folder. So, you could declare the path in gradle.properties like org.gradle.java.home=C:\Program Files\Java\[or yours jdk folder name].

Use this guide for using gradle.properties.

Or (and I prefer this solution) include JAVA_HOME path in systmem variables and restart CMD.

enter image description here

Sampa
  • 181
  • 1
  • 6
  • Tank you. for those of us that want a solution to work on multiple operating systems with different file paths this is the answer. – chrisroker0 Mar 24 '22 at 21:53
4

If you can not find Tools.jar in JAVA_HOME, You can use this method. But you have to have Android Studio in your PC.

org.gradle.java.home=C:\\Program Files\\Android\\Android Studio\\jre

Place this in your project > android > gradle.properties

Lakpriya Senevirathna
  • 2,488
  • 2
  • 17
  • 35
2

What I did was I uninstalled Java from my PC, and then downloaded and installed JDK again from Oracle. After this it worked perfectly. I think the problem was because the JRE and JDK update version were different from each other.

romin21
  • 1,532
  • 1
  • 14
  • 35
2

My error was solved by uninstalling all java updates and java from control panel and reinstalling JDK

haidar
  • 1,449
  • 1
  • 11
  • 12
2

My issue was two-fold:

  1. I had the JRE installed, but not the JDK.
  2. I did not have a JAVA_HOME environment variable set.

Note: I am using Fedora Linux.

To resolve the first issue, I followed these instructions

To resolve the second, I merely added the following line to my ~/.bashrc file:

export JAVA_HOME=/usr/lib/jvm/java/

I had to restart my IDE and terminals to ensure the change to the ~/.bashrc took affect.

scottysseus
  • 1,922
  • 3
  • 25
  • 50
2

Solution 1:

  1. Go to your android folder > Gradle.properties > add your jdk path.

  2. Clean and rebuild then it is done. // For Example Purpose Only

    org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_251.jdk/Contents/Home

Solution 2 At last, here I found the solution.

  1. Add jdk path

    to gradle.properties file and did a rebuild.
    
  2. This will also solve your error.

Here is all Solution could not find tools.jar

piotrek1543
  • 19,130
  • 7
  • 81
  • 94
1

Please make sure both JDK and jre are on same version for example if you have JRE version 1.8.0_201 then JDK version should be 1.8.0_201 version.

vishal jangid
  • 2,967
  • 16
  • 22
1

What I did was download the JDK from here, start a windows command prompt (windows+r CMD) and set the environment variable JAVA_HOME to c:\Program Files\Java\jdk-14 with:

set JAVA_HOME="c:\Program Files\Java\jdk-14"

Then run what I wanted to run. It failed afterwards, but on a different issue.

user27221
  • 334
  • 3
  • 16
1

In my case it was because the JAVA_HOME path could not find it.

  • Close all terminals and open a new one

  • Find a directory like this on your Mac: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home

  • What may change is the version, but once you have it, it runs in that path: export JAVA_HOME = / Library / Java / JavaVirtualMachines / jdk1.8.0_91.jdk / Contents / Home

IN MY CASE I RUN THIS COMMAND AT THE ROOT OF THE REACT NATIVE PROJECT:

export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

AND IT WORKED.

Caro Pérez
  • 476
  • 4
  • 4
1

I faced the same problem when I installed the new android studio version and this solution works for me, so if you don't have JDK installed in the java folder or isn't the same version as JRE then you should install it (should JRE and JDK be in the same version, for example, both of them be in this version 1.8.0).

install it from this link => download JDK from Oracle

then copy tools.jar from (JDK folder) ==> jdk1.8.0../lib and paste it in (JRE folder) ==> jre1.8.0../lib

Amina Bekir
  • 220
  • 2
  • 9
1

To Solve ERROR: Could not find tools.jar. Please check that C:\Program Files\Java\jre1.8.0_151 contains a valid JDK installation Error You Just need to add the java home JDK path in gradle.properties Just like this: org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_144 Then try to rebuild project. Now your error must be solved.

The second solution is You just need to add tools.jar file to C:\Program Files\Java\jre1.8\lib this Path. First of all Copy tools.jar file from C:\Program Files\Java\jdk1.8.0_121\lib and then Paste into C:\Program Files\Java\jre1.8\lib Here. Now your error must be solved

Dr Mido
  • 2,414
  • 4
  • 32
  • 72
0

for tools.jar is in C:\Program Files\Java\jdk1.8.0_201\lib. I changed installed jre in eclipse. Windows->Preferences->Java->installed JREs and make default jre to point to where your jdk is.

0

This can occur if your path is too long as well. I solved this by moving my java install to

C:\Java\jdk1.8.0_211

cblanto7
  • 168
  • 1
  • 3
  • 12
0

What worked for me was updating Android Studio and updating JAVA_HOME and ANDROID_HOME environment variables. I believe it was caused due to the fact that I updated Java Version (through updater) but did not update jdk.

0

In Eclipse

Right click Project --> Java Build Path --> Libraries

  1. Remove the JRE
  2. Click on Add Library --> JRE System Library -->Next -->Alternate JRE --> installed JRE --> Add JDK and select and Apply
upog
  • 4,965
  • 8
  • 42
  • 81
0

you have to added jdk path org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_102 to gradle.properties make sure you write your jdk version which installed in you system.

0

In my case, i had installed only the JRE so you could check to make sure you actually have a valid jdk. if not i advise you to uninstall whatever java is installed and download a correct jdk from here (the jdk comes with a jre so no need to download anything else) after set the environment variable and your done

ochieng seth
  • 59
  • 1
  • 14
0

Goto android folder -> gradle.properties -> add your java sdk path

org.gradle.java.home=/Users/userName/Library/Java/JavaVirtualMachines/corretto-1.8.0_302/Contents/Home/jre
Navin Kumar
  • 3,393
  • 3
  • 21
  • 46
0

I had to add system environment variable JAVA_HOME and restart workspace. It solved the problem.

Prashant S
  • 349
  • 4
  • 14
0

First of all you guys needs to check on your tool versions. below version may solve most of the problems.

  1. jdk - v11
  2. node - v16.13.1
  3. Gradle - v6.1.1
Dijal Tom
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 03 '22 at 02:09
0

After I've read the threads above, there are three approaches I've tried that work for me:

  • open system variable environment, add a new system variable named JAVA_HOME and set its value to your JDK path containing the 'tools.jar' file e.g. C:\Program Files\Java\jdk1.8.0_311.
  • create a gradle.properties file in your project and add the following line org.gradle.java.home=<your JDK path e.g. C:\\Program Files\\Java\\jdk1.8.0_311>
  • copy the "tools.jar" file from your JDK folder and paste it in the JRE folder that caused the error. You could find your jar file in lib folder of your jdk path e.g. C:\Program Files\Java\jdk1.8.0_311\lib
lambo9495
  • 5
  • 4
-1

I tried several of the above ways, but none worked for me, try the following; download the following java version Link download in Oracle, after downloading and installing it, add this to the gradle.properties file org.gradle.java.home = C: \\ Program Files \\ Java \\ jdk1.8.0_301 and that's it, restart the vs code and simulate again, and it worked!

Eric Aya
  • 69,473
  • 35
  • 181
  • 253