180
FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':react-native-linear-gradient:compileDebugJavaWithJavac'.
> Could not find tools.jar. Please check that /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home contains a valid JDK installation.

When I upgraded to Mac os Big sur and run,

npx react-native run-android

I got this error (Android). I have tried a lot of solutions from Stack Overflow, but none of them worked.

I have created a fresh project and it's working. Also some of the old projects are also working perfectly.

*react-native Version: "0.63.3",*

Please help me to find a solution?

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Anandu Viswan
  • 1,803
  • 2
  • 5
  • 4
  • Does this answer your question? [Gradle does not find tools.jar](https://stackoverflow.com/questions/11345193/gradle-does-not-find-tools-jar) – emkarachchi Dec 04 '20 at 02:38
  • Similar [Ionic + Cordova Android build error - could not find tools.jar](https://stackoverflow.com/questions/64856188/ionic-cordova-android-build-error-could-not-find-tools-jar) – tomfriwel Dec 09 '20 at 06:27
  • 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:31
  • Remember to quit Terminal app before rebuilding, otherwise JAVA_HOME will not get updated – Vinvolv Dec 23 '22 at 08:02

24 Answers24

394

The problem is that with the update the built-in java took precedence and it doesn't have the SDK because it's just the runtime.

You just need to change your java home and add the java binary to your .zshrc to find your java home execute:

/usr/libexec/java_home -V | grep jdk

the output should be similar to the following:

Matching Java Virtual Machines (1):
1.8.0_272 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home
/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

you should take the path from the one that says SDK in my case

/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

after that, you just add the following to the end of your .zshrc that should be in your home.

You can edit it with (if you decide to use vim you can exit writing :wq! and pressing enter)

vim .zshrc

add the following:

export JAVA_HOME=the/path/you/copied/before
export PATH=$JAVA_HOME/bin:$PATH

where the/path/you/copied/before in my case would be

/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

save the file and close all your terminals and open them again.

If while editing .zshrc file getting error ".zshrc" E212: Can't open file for writing then use sudo vim.zshrc and you'll be able to edit.

The error should be solved.

Edit

Instead of ~/.zshrc, you could have ~/.bash_profile or ~/.bash_rc so edit yours accordingly

TSR
  • 17,242
  • 27
  • 93
  • 197
Chris Gomez
  • 6,644
  • 4
  • 18
  • 39
  • 50
    Thanks for that worked perfectly. I think the issue happened for me after updating to Big Sur – Fazberry Nov 26 '20 at 12:17
  • 10
    Thanks!!!! I had this problem for three weeks, after update to Big Sur – Israel Alfaro Dec 05 '20 at 20:14
  • After attempt to save file zshrc in vim I got ---> ".zshrc" E212: Can't open file for writing – Maxim Petlyuk Dec 27 '20 at 13:16
  • @МаксимПетлюк you can use any other text editor of your preference. – Chris Gomez Dec 27 '20 at 23:19
  • @CristianGomez yep, but the point is that seems mac os don't wont to give me opportunity to change that file. Even in another text editor, even after giving write permissions to it and so on. – Maxim Petlyuk Dec 28 '20 at 13:33
  • @МаксимПетлюк check if you're in your home directory when doing it, if the file doesn't exist, create and edit it in your home directory /User/YourUser/.zshrc – Chris Gomez Dec 29 '20 at 09:28
  • 1
    After applying these changes, if you don't want to close the terminal, simply hit `source .zshrc` – kavrosis Feb 25 '21 at 14:53
  • I had to source ~/..zshrc after restarting the terminal to get it working correctly. – Satheesh Mar 10 '21 at 13:59
  • Thanks for this answer! If I may expand upon it slightly, I currently set JAVA_HOME=`/usr/libexec/java_home -v 1.8`. However, the update to Big Sur added /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home as the latest path returned for '1.8'. Updating the -v filter to '1.8.0' allowed the java_home command to find the correct path again. – dafunker Mar 22 '21 at 14:51
  • it doesn't have to be .zshrc, in my case it's bash_profile, it could be .bashrc as well – Stanislau Buzunko May 29 '21 at 16:45
  • I was a few seconds from trying to just reinstall Android studio but then this worked. Youre the best! – Display name Sep 24 '21 at 20:59
  • By the way, you don't need to do this in `vim`, you can just use vscode. just replace `vim` with `code` like this: `code ~/.zshrc` or `sudo code ~/.zshrc` and the file will be opened in vscode (obviously vscode is required for this to work). – Juan Castano Oct 03 '21 at 05:19
  • 1
    @dafunker How does the update allow the java_home to find the correct path? For me the `/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home` path is the only path it returns, and there's nothing in the `/Library/Java/JavaVirtualMachines/` directory. – 0xTomato Jan 10 '22 at 23:47
  • @0xTomato I think if your `/Library/Java/JavaVirtualMachines/` is empty it implies that you don't have any JDK's installed. `/usr/libexec/java_home -V` will show you all your available options. – dafunker Jan 31 '22 at 11:40
125

In my case:

I used the command /usr/libexec/java_home -V | grep jdk Cristian Gomez provided, got two paths:

Matching Java Virtual Machines (2):
    1.8.181.13 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_111 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home

Changing the .zshrc did not solve the problem.

Finally I copied tools.jar in /Library/Java/JavaVirtualMachines/jdk1.8.0_111.jdk/Contents/Home/lib/ to /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib/ and solved the problem.

I don't know if this approach will lead to any unknown problems.

tomfriwel
  • 2,575
  • 3
  • 20
  • 47
  • 3
    Worked for me to. No idea when this gonna blow up in my face... – samzmann Dec 17 '20 at 10:59
  • 13
    Setting JAVA_HOME didn't work for me either. Copying tools.jar did work, but like both of you said could cause issues down the road. Instead I used Java's uninstall tool (https://www.java.com/en/download/uninstalltool.jsp) to uninstall Java from my mac. Java only knew of its internet plugins location, and wasn't aware of the other 2 versions of java installed that terminal already knew about (virtual machines). When it uninstalled, it only removed the internet plugins one. The java_home auto reset to one of the other folders. Now I have one less version of java installed, but no file copying. – Heinous Games Dec 22 '20 at 03:47
  • this solved my issue too...you saved alot – Amani Dec 28 '20 at 19:09
  • Worked for Ionic5 – vr_driver Feb 13 '21 at 06:50
  • 1
    Also ended up moving 'JavaAppletPlugin.plugin' to another folder, java_home reverted to one of the other paths listed, works for now. – halfpad Feb 15 '21 at 14:28
  • You didn't solve your original problem. You only copied JDK file into JRE. – m1ld Mar 03 '21 at 10:45
  • Worked like charm! – Mayur Baldha Apr 29 '21 at 10:05
  • This solved my issues too with gradle build. Thanks a ton! – rustygb May 12 '21 at 00:08
  • Setting JAVA_HOME to the second path that points to "Java SE 8" JDK worked for me. – Mark May 13 '21 at 18:14
  • JavaAppletPlugin.plugin it's a file in my macos, how could you use it as a folder? – Nemus Jul 29 '21 at 10:43
  • 2
    @Nemus Right-click JavaAppletPlugin.plugin and select "Show Package Contents". – tomfriwel Jul 30 '21 at 05:27
39

This worked for me -

  1. Goto android folder > gradle.properties file > add below line org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home // path to JDK

  2. Run your project again

Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Yokesh Sankar
  • 399
  • 3
  • 2
9

If you have something in your ~/.zshrc that resembles this:

export JAVA_HOME=$(/usr/libexec/java_home -v1.8)

just like tomfriwell wrote, you can run:

/usr/libexec/java_home -V

to see the paths of your installed JVMs and then specify which one you'd like to use. e.g:

export JAVA_HOME=$(/usr/libexec/java_home -v1.8.0_261)

and that'll link the desired JVM to your java command

don't forget to source ~/.zshrc after editing and saving

RoyalBigMack
  • 620
  • 7
  • 7
9

As I do not have ~/.zshrc on my mac these steps fixed the issue:

1)To find your java home execute:

/usr/libexec/java_home -V | grep jdk

and I see this:

Matching Java Virtual Machines (2):
1.8.271.09 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
1.8.0_171 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home

2)To edit .bashrc I used this command:

nano ~/.bashrc

and I see the next content of my ~/.bashrc:

# Add RVM to PATH for scripting. Make sure this is the last PATH variable change.
export PATH="$PATH:$HOME/.rvm/bin"

3)I added 2 strings at THE END of the ~/.bashrc:

export JAVA_HOME="/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home"
export PATH="$JAVA_HOME/bin:$PATH"

where JAVA_HOME path - "/Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home" is taken from STEP 1(!):

1.8.0_171 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_171.jdk/Contents/Home

Please note - paths should be inside of quotes ""

4)Close all terminal windows and build Android again.

Stich
  • 2,331
  • 1
  • 15
  • 31
6

This worked for me: Start Java from fresh installation.

sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -rf /Library/Java/JavaVirtualMachines
sudo rm -rf /Library/Application\ Support/Oracle/Java/
sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane

Restart computer.

Download and install Java jdk-8u271-macosx-x64

Restart computer.

daozi
  • 99
  • 1
5
  1. /usr/libexec/java_home -V | grep jdk

Matching Java Virtual Machines (2): 1.8.152.16 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home 1.8.0_152 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_152.jdk/Contents/Home

  1. Open "/Library/Internet Plug-Ins", remove JavaAppletPlugin.plugin

  2. OK

xushao
  • 93
  • 1
  • 2
4

If you are on mac & tool.jar not found. Then first check for JAVA_HOME in your .zshrc or .bashrc.

If it is present then change the line with the below line & if it isn't present add it.

export JAVA_HOME=/Applications/Android\ Studio.app/Contents/jre/jdk/Contents/Home 

After adding the above line run the below command.

source ~/.zshrc

or

source ~/.bashrc

Note: Make sure you have installed Android Studio already.

Rahul
  • 474
  • 5
  • 8
4

When you have 2 Javas on your system,

    ~ % /usr/libexec/java_home -V | grep jdk
Matching Java Virtual Machines (2):
    1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
    1.8.0_144 (x86_64) "Oracle Corporation" - "Java SE 8" /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home

Try to add the "Java SE" path into your gradle.properties as only that package contains tools.jar

org.gradle.java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home
goodhyun
  • 4,814
  • 3
  • 33
  • 25
3

This is because valid jdk path could not be detected. Go to your project, open android folder > gradle.properties file > add below line

org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/jdk/Contents/Home

run your project again react-native run-android

Original Answer: Link

Ashwin J
  • 662
  • 7
  • 12
  • This works (yay!!), but folks may want to double check their path: I opened `Applications`, right-clicked on 'Android Studio.app' selected 'Show Package Contents', then navigated to /Contents/jre. I found on my system there is no jdk, so my path was: `org.gradle.java.home=/Applications/Android Studio.app/Contents/jre/Contents/Home` – Keith DC Aug 08 '21 at 22:26
3

Run this command on terminal

/usr/libexec/java_home -V | grep jdk

Output

Matching Java Virtual Machines (2):
        1.8.291.10 (x86_64) "Oracle Corporation" - "Java" /Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home
        1.8.0_292 (x86_64) "AdoptOpenJDK" - "AdoptOpenJDK 8" /Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

Copy tool.jar from

/Library/Internet Plug-Ins/JavaAppletPlugin.plugin/Contents/Home/lib

and paste to

/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/lib

Command to install JDK

brew install --cask adoptopenjdk/openjdk/adoptopenjdk8

Add following paths to .base_profile and .zshrc under Home directory

export ANDROID_HOME=$HOME/Library/Android/sdk
export PATH=$PATH:$ANDROID_HOME/emulator
export PATH=$PATH:$ANDROID_HOME/tools
export PATH=$PATH:$ANDROID_HOME/tools/bin
export PATH=$PATH:$ANDROID_HOME/platform-tools
Kirit Vaghela
  • 12,572
  • 4
  • 76
  • 80
2

Try setting JAVA HOME in android/gradle.properties file. This worked for me.

More info at https://dev.to/larrydanny/could-not-find-tools-jar-4363

Parameshwar Ande
  • 807
  • 1
  • 8
  • 16
2

Using /usr/libexec/java_home -V | grep jdk to find 2 locations then

Removing JavaAppletPlugin.plugin from "/Library/Internet Plug-Ins" worked for me.

Solvik
  • 51
  • 2
1

on my mac big sur SSD, Simply downloading jdk helped

Siddharth Sogani
  • 321
  • 1
  • 10
0

Upgrading Android Studio worked for me

Dazzle
  • 2,880
  • 3
  • 25
  • 52
0

I go to java in MAC System preferences try to update java ask to remove unused version after removed finally WORKED

Issa Lafi
  • 345
  • 3
  • 3
0

JDK 16 doesn't have tools.jar file.

  1. Download JDK 8, copy tools.jar file from /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib
  2. Past it JDK 16 folder at /Library/Java/JavaVirtualMachines/jdk1.8.0_291.jdk/Contents/Home/lib
0

If you are using Unity:

Open Preferences -> External Tools Under Android/JDK click on Copy Path enter image description here

then open the terminal set this as your Java home:

export JAVA_HOME="{Paste value with quotes}"
Fernando Bonet
  • 576
  • 5
  • 13
0

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

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 the same issue but it was because I installed too many JDk's. Im on MAC M1, and was not sure what jdk should I use. Newest JDK 17 was not supported by my project, I needed JDK 8 ARM. I got is from here https://www.azul.com/downloads/?version=java-8-lts&os=macos&package=jdk

But I got the same problem as OP. But it was because I had too many versions of JDK, and I have no idea where Android studio is taking the info which JDK to use.

Used /usr/libexec/java_home -V to find all the JDK's and manually deleted thema all except the ARM version.

The JDK's are installed in Macintosh HD / Library , not in Users.

paakjis
  • 369
  • 4
  • 16
0

My problem was the that zsh and bash does not have the same path to to the jdk. So as an additional sanity check you can check what terminal you are running. Use the one the PATH was set up with.

-1

Reinstall java jdk worked for me.

in Terminal:

brew tap adoptopenjdk/openjdk
brew install --cask adoptopenjdk8
STA
  • 30,729
  • 8
  • 45
  • 59
-1

I have used every method and found out what worked for me so I have decided to compile it down for others. As told by @Heinous_Games in the comment section, install Java Uninstall Tool and it will remove Java from your system and to make sure that it works run this manually from your terminal :

sudo rm -rf /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
sudo rm -rf /Library/Java/JavaVirtualMachines
sudo rm -rf /Library/Application\ Support/Oracle/Java/
sudo rm -rf /Library/PreferencePanes/JavaControlPanel.prefPane

Go to Java Downloads page and download the file suitable for your system in my case "macOS x64 DMG Installer" and follow the instructions. I hope it works and if it doen't then restart your system once.

abdemirza
  • 629
  • 4
  • 16
-4

Upgrade Android Studio to 4.0, and you are good to go