Issue
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?
Solution
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
Answered By - Cristian Gomez
Answer Checked By - Marilyn (JavaFixing Volunteer)