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
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.
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.
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.
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.
My issue was two-fold:
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:I had to restart my IDE and terminals to ensure the change to the
~/.bashrc
took affect.At last, here I found the solution.
I added jdk path
org.gradle.java.home=C:\\Program Files\\Java\\jdk1.8.0_144
togradle.properties
file and did a rebuild. It works now.