I've followed step by step the official Getting Started. I started from a clean linux install and installed everything required as per the "Building Projects with Native Code" tab. I have also read the troubleshooting section. I've already created the project using the terminal.
This is the error when I run react-native run-android:
Starting JS server...
Building and installing the app on the device (cd android && ./gradlew installDebug)...
ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
Could not install the app on the device, read the error above for details.
Make sure you have an Android emulator running or a device connected and have
set up your Android development environment:
https://facebook.github.io/react-native/docs/android-setup.html
It does not find JAVA_HOME because the latest versions of Android Studio don't require Java to be installed in the system. Instead an internal JRE is used.
Duplicate disclaimer: I've already read this question. This is not what I want. I know how to set the Java home. I just want to run the react project WITHOUT having to install a separate Java.
Questions:
How could I find the internal Java inside the Android Studio folder so that I could point JAVA_HOME to it?
If not possible, could I open and run the project inside the android folder with Android Studio? How would I refresh this project after modifying the React JavaScript code in the parent folder?
Windows 10:
Android Studio -> File -> Other Settings -> Default Project Structure... -> JDK location:
copy string is shown, such as:
C:\Program Files\Android\Android Studio\jre
In the file locator directory window, right-click on "This PC" ->
Properties -> Advanced System Settings -> Environment Variables... -> System Variables
click on the New... button under System Variables, then type and paste respectively:
.......Variable name: JAVA_HOME
.......Variable value: C:\Program Files\Android\Android Studio\jre
and hit the OK buttons to close out.
Some installations may also require JRE_HOME to be set in the same way.
To check, open a NEW black console window, then type echo %JAVA_HOME%. You should get back the full path you typed into the system variable. Windows 10 supports spaces in the filename paths for system variables very well and does not need ~tilde eliding.
I'll answer my own questions and sponfeed my fellow linux users:
1- To point JAVA_HOME to the JRE included with Android Studio first locate the Android Studio installation folder, then find the /jre directory. That directory's full path is what you need to set JAVA_PATH to (thanks to #TentenPonce for his answer).
On linux, you can set JAVA_HOME by adding this line to your .bashrc or .bash_profile files:
export JAVA_HOME=<Your Android Studio path here>/jre
This file (one or the other) is the same as the one you added ANDROID_HOME to if you were following the React Native Getting Started for Linux. Both are hidden by default and can be found in your home directory. After adding the line you need to reload the terminal so that it can pick up the new environment variable. So type:
source $HOME/.bash_profile
or
source $HOME/.bashrc
and now you can run react-native run-android in that same terminal. Another option is to restart the OS. Other terminals might work differently.
NOTE: for the project to actually run, you need to start an Android emulator in advance, or have a real device connected. The easiest way is to open an already existing Android Studio project and launch the emulator from there, then close Android Studio.
2- Since what react-native run-android appears to do is just this:
cd android && ./gradlew installDebug
You can actually open the nested android project with Android Studio and run it manually. JS changes can be reloaded if you enable live reload in the emulator. Type CTRL + M (CMD + M on MacOS) and select the "Enable live reload" option in the menu that appears (Kudos to #BKO for his answer)
I think the right way to find the internal Java used by the Android Studio is to
Open Android Studio
Go to File->Other Settings->Default Project Structure/JDK Location:
and copy what ever string is specified there
This will not require memorising the folder or searching for java and also these steps wil take of any future changes to the java location by the Android Studio team changes I suppose
I'm using Ubuntu 20 on my machine, I have got the same problem the thing that worked for me was:
sudo apt-get install default-jre
you can verify that if the installation was successful or not by:
java --version
you are gonna see something like this in your terminal
openjdk 11.0.12 2021-07-20
OpenJDK Runtime Environment (build 11.0.12+7-Ubuntu-0ubuntu3)
OpenJDK 64-Bit Server VM (build 11.0.12+7-Ubuntu-0ubuntu3, mixed mode, sharing)
It is located on the Android Studio folder itself, on where you installed it.
Please add the JAVA_HOME in the System variable no in the user variable
Create the Variable name as JAVA_HOME
Please use these format in the value box --> C:\Program Files\Java\jdk(version) what you have or downloaded.
I fixed this issue by installing jre, I have jdk already installed but jre was not installed.
For those still not able to set JAVA_HOME from Android Studio installation, for me the path was actually not in C:\...\Android Studio\jre
but rather in the ...\Android Studio\jre\jre.
Don't ask me why though.
Environment variables jre location snippet
For ubuntu 16.0+
You can find the jre folder in android studio on this path /snap/android-studio/current/android-studio/jre
add the following line in your .bashrc or .bash_profile
export JAVA_HOME=/snap/android-studio/current/android-studio/jre
type
sudo nano $HOME/.bashrc
or
sudo nano $HOME/.bash_profile
after this execute the following line to in your terminal where you are executing the npx react-native run-android to reload the terminal with the new changes
source $HOME/.bashrc
or
source $HOME/.bash_profile
All I did is go to my project directory from the cmd (command prompt) I typed java -version.it told me what version it was looking for. so I Installed that version and I changed the path to were the jdk of that version was located .
apparently the system variables were not in sync with the user
variables
run as admin on command prompt and set the variable
i also saw some tips to close the node window and the vscode terminal for
the variable to take effect
tip from this site
https://confluence.atlassian.com/doc/setting-the-java_home-variable-in-windows-8895.html
setx -m JAVA_HOME "C:\Progra~1\Java\jdk1.8.0_XX"
and then
echo %JAVA_HOME%
For fish users:
Open ~/.config/fish/config.fish;
Include line set JAVA_HOME /home/ronald/Documents/android-studio/jre and set PATH $PATH $JAVA_HOME;
Re-open fish terminal.
Welcome :)
After adding the environmental variable , please restart the editor. It may save your ton of time.
Make sure you have java installed
your path is wrong
do this:
export | grep JAVA
THE RESULT: what java home is set to
JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
follow the path to see if the directories are correct
i did this in my terminal:
open /Library
then i went to /Java/JavaVirturalMachines
turns out I had the wrong "jdk1.8.0_202.jdk" folder, there was another number...
4. you can use this command to set java_home
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home
I ran this in the command prompt(have windows 7 os):
JAVA_HOME=C:\Program Files\Android\Android Studio\jre
where what its = to is the path to that jre folder, so anyone's can be different.
what I did to solve this on windows was to go to the environment variables and set the variable name to JAVA_HOME and then set the variable value to C:\Program Files\OpenJDK\openjdk-8u312-b07
then I set the path to C:\Program Files\OpenJDK\openjdk-8u312-b07\bin
From Android Studio, go to File, Settings, "Build, Execution, Deployment", Build Tools, Gradle. You should see the path to your JDK there, as visible in the screenshot (tested on Android Studio version 2020.3.1). Set an environment variable called JAVA_HOME containing that path (just google "set an environment variable" and followed by your Operating System name).
for those who are new to react native development
please set JAVA_HOME like this
instead of
C:\Program Files\Java\jdk1.8.0_301\bin
just remove "\bin" from path
C:\Program Files\Java\jdk1.8.0_301 after removing \bin
Guys after fight a lot to find the solution, including pass for many of that articles. I just realized that when you extract the Android SDK it doesn't come inside the folder cmdline-tools/latest and doesn't matter the place that I put the folder, opt/Androd_SDK, $HOME/Android_SDK the sequence of the error were the same. Until I got this error running:
➜ bin ./sdkmanager -version
Error: Could not determine SDK root.
Error: Either specify it explicitly with --sdk_root= or move this package into its expected location: /cmdline-tools/latest/
So, basic the solution was add the content of the SDK zip inside of the folders cmdline-tools/latest.
Once you do this, SDK and all the commands will work normally. Just don't forget to finish the configuration, like create the environment of the Android SDK, JAVA and so on.
An awesome article is that one:
https://medium.com/#khairold/setting-up-react-native-on-linux-without-android-studio-a65f3e011bbb
;)
if someone still facing similar issue and looking for an answer for the same? I would like to share the solution which worked for me. And by the way I haven't installed JAVA JDK/SDK in order to resolve the issue.
I am using Visual Studio code to develop react-native apps (using nox player for android app testing), I ran into the similar issue:
Issue:
$
› Building app...
$
ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
$
Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.
C:\Users...\android\gradlew.bat exited with non-zero code: 1
Resolution: In package.json file I have changed "main": "index.js" to "main": "node_modules/expo/AppEntry.js" it worked for me.
It would be great if this solution helps anyone, let me know for any additional information.
Make sure you have installed the following:
Java
JDK
JRE
If you use Ubuntu, maybe its helpful this tutorial. You have solution for how to solve error ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH..
There are many solutions found for Windows or Mac OS on internet to resolve this error. I'm using Ubuntu 22.10 kinetic
Solution:
Go to your Android Studio folder, find folder named jbr, copy all content folder, and if you don't have, create a folder named jre and copy in jre folder, all contents from jbr folder. Case close. Error solved.
java is not set
Video
guys . i just try to install react native in my laptop. i've followed all setup instruction but i stiil get those error .
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Could not find tools.jar
i used genymotion with os version 5.1.0, i've setup environment variable for android_home. Any idea what i miss here ?
Setting the missing "JAVA_HOME" system environment variable worked for me.
Press WIN+Pause on keyboard.
Click advanced system settings.
Click "Environment variables".
Click "New" under "System variables".
Type "JAVA_HOME" in the Variable name field.
Type the path to the JDK in the Value field. For me the correct path
with short filename was "C:\PROGRA~1\Java\jdk1.8.0_66". Your path will vary depending on the installed version.
Click OK, and OK in previous window too.
Restart any command prompts or restart the computer.
react-native run-android worked fine after that.
Setting up appropriate JDK (not JRE. JRE does not have tools.jar) version (E.g. JDK 1.7x) path as the JAVA_HOME would resolve this issue.
Please check your computer's environment variables and set JAVA_HOME if it has not already been setup.
If you have the Android Studio embedded JDK, then it may cause conflicts. A workaround for this problem I've found is setting your JAVA_HOME to the default JDK location specified in Android Studio.
Open Android Studio
Press Ctrl+Alt+Shift+S to open Project Structure window.
Set your JAVA_HOME environment variable to the path specified under "JDK location".
I'm gonna go on a long shot here.
There is a tools.jar in java libs folder.[the java sdk that is]
Usually all these frameworks and ides have a lib folder themselves. and it is missing said tools.jar. Just copy paste it there.
I had the same problem while running react-native run-android it gave me the error tool.jar not found. i solved this by adding a single line mentioned below, in gradle.properties file which is in android project.
org.gradle.java.home=C:\Program Files\Java\jdk1.8.0_121
If this problem occurs for Windows, then the solution here is fairly simple.
Install Chocolatey
Follow the installation instructions on this page. https://chocolatey.org/install
Install jdk8
Restart Command Prompt or Powershell as Administrator, and run:
choco install jdk8
Now if you run
react-native run-android
This error shouldn't show up.
Find and copy tool.jar from JDK path
C:\Program Files\Java\jdk1.8.0_151\lib\tool.jar
then paste it in JRE folder
C:\Program Files\Java\jre1.8.0_181\lib
then delete build folder from your project/android/app/=>build
and then try react-native run-android
Just to add another data point for people pulling their hair out. I think a java update may have messed up my environment that was working fine 2 months ago. The combination of what worked for me:
Set JAVA_HOME to 1.7, NOT 1.8.
I had 2 1.8 versions installed and neither worked.
I know this question is for Windows, but this is the first result for this error on Google, so for Linux this problem could be fixed with:
sudo apt install default-jdk
I have deleted "\react_native_project_path\android" directory and run
reactive-native android
then run ..
reactive-native run-android
Then the error is gone
At first I have to tell you open your CMD Then write java -version then write javac -version Then see IF you haven't them press Win + pause then click on advance system setting then click on Environment variables then in system variables Click on New Then write JAVA_HOME in variable name and in the second section write your JDK path and click ok then in system variable click on path then click edit and then click on New and write %JAVA_HOME%\bin and click ok then ok and at the end if you want to check it open CMD again and write java -version and then javac -version I hope this will work for you.
I have read all the questions for the same problems and I have tried everything but nothing is working for me when I try to add android platform for cordova.
c:\Apps\fapp>cordova platform add android
Creating android project...
C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node_modules\q\q.js:126
throw e;
^
Error: An error occurred while listing Android targets
at C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\lib\check_reqs.js:
87:29
at _rejected (C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node_mo
dules\q\q.js:808:24)
at C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node_modules\q\q.j
s:834:30
at Promise.when (C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node
_modules\q\q.js:1079:31)
at Promise.promise.promiseDispatch (C:\Users\Andrej\.cordova\lib\android\cor
dova\3.4.0\bin\node_modules\q\q.js:752:41)
at C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node_modules\q\q.j
s:574:44
at flush (C:\Users\Andrej\.cordova\lib\android\cordova\3.4.0\bin\node_module
s\q\q.js:108:17)
at process._tickCallback (node.js:415:13)
Error: cmd: Command failed with exit code 8
at ChildProcess.whenDone (C:\Users\Andrej\AppData\Roaming\npm\node_modules\c
ordova\src\superspawn.js:112:23)
at ChildProcess.EventEmitter.emit (events.js:98:17)
at maybeClose (child_process.js:743:16)
at Process.ChildProcess._handle.onexit (child_process.js:810:5)
Project is created using cordova create fapp com.fapp Fapp
System is windows 8
ANT, JAVA (JDK 1.7.0_51 and JRE7), Android SDKs are installed from (17-19.0.3) and Android APIs (17-19)
All environment variables for JAVA, ANDROID and ANT are set as well as path to bin for all of them into Path variable.
I really tried everything but keeps throwing this problem.
EDIT:
Answer here did not help:
cordova ubuntu: An error occurred while listing Android targets
Permissions are set to Everyone full control and same issue
FINAL SOLUTION
Finally I re-installed JDK and Android SDK and now everything works even it is still mystery to me.
I had the same issue and solved it by setting the following environment variables:
%JAVA_HOME% points to the root of your java installation (not the bin directory). In my computer it is: C:\Program Files\Java\jdk1.8.0. To test that it is configured correctly, open a new terminal and type javac. It should run the java compiler and shouldn't complain that it doesn't know the command. Please note that environment variables are only set when opening a new terminal (at least on windows) so every time you change something you have to open a new terminal.
%ANT_HOME% point to the root of where you've installed Apache ant. In my computer it is: C:\Users\User\apache-ant-1.9.3. Test it by running the ant command. It shouldn't complain that the ant command is unknown to your machine. Remember to open a new terminal after setting the path and then try running ant.
%ANDROID_HOME% points to where you have installed your android SDK. For example: C:\Users\User\android\sdk. Then try typing emulator to make sure it is installed correctly.
When all of these variables are set, you can use them in the PATH environment variable :
%PATH% contains the following entries: %ANT_HOME%\bin;%ANDROID_HOME%\tools;%ANDROID_HOME%\platform-tools;%JAVA_HOME%\bin
On windows 8 you can configure path and variables by right clicking on the start button, choosing system, and then advanced system settings and clicking on environment variables button. Remember to define the above environment variables above PATH. I defined everything in the user variables and it works just fine.
-Possible solution for those who encounter this error
I had this error and it did relate to the path variables. I thought I had everything set correctly, but I was missing a portion.
First check by typing the command android into command line. If the SDK isn't opened you have not set the variables and/or path correctly.
You should have 3 (or 4 if you have separate android ones) variables.
ANT_HOME, JAVA_HOME and ANDROID_HOME
ANDROID_HOME should point to the sdk. If you (like I did) downloaded the bundle which includes eclipse, you need to have this variable point to the sdk folder inside the android bundle folder, not the android bundle folder itself (eg. C:\Dev\adt-bundle-windows\sdk)
You need to add %ANDROID_HOME%\tools and %ANDROID_HOME%\platform-tools to your path. Along with the link to the ANT bin folder and jdk bin.
I strongly urge you to triple check that all variables are correctly set AND that all four parts are added to the path.
Please Refer the Question
An error occurred while listing Android targets
and the Link
developer.android.com/tools/devices/managing-avds-cmdline.html
Most probably the path variable pointing to this android was wrong.
I had used %ANDROID_HOME% as variable and scaled the same to path variables %ANDROID_HOME%\SDK\TOOLS. This method doesn't work. Experimentally resolved by directly pointing to the Tools and Platform Tools.
Just based on what I did and it worked for me. Hope You get the Issue resolved.
I had the same issue with version 3.5.0.
The problem is, that the error message from the script check_reqs located in C:\Users\{yourUserName}\.cordova\lib\android\cordova\3.5.0\bin, is not forwarded correctly.
I run the mentioned script manually and got the following error message:
[Error: Please install Android target 19 (the Android newest SDK). Make sure you have the latest Android tools installed as well. Run "android" from your command-line to install/update any missing SDKs or tools.]
So basically to solve this issue you have to install the latest Android SDK.
This link http://spring.io/guides/gs/android/ + running cmd as an administrator solved that issue for me.
This confirms Michael Brooks' assumption that this issue has to do with permissions settings on the Windows OS. https://github.com/phonegap/phonegap-cli/issues/77
I've read this: Cordova and setting Android using command line
and that How to add android to cordova platform? PATH error? and generally speaking whole documentation. I've added both 'platform-tool' and 'tool' paths, installed android 4.4 and I'm still getting the error from the tittle when I'm trying cordova platform add android to my current project ( in cmd and in netbeans ).
The only thing I haven't done is adding java and ant to path. I was trying with ;%JAVA_HOME%\bin;%ANT_HOME%\bin but still those commands aren't recognizable.
What else can I do?
EDIT:
I've added java and ant to PATH. This is what I get in cmd:
C:\xampp\htdocs\mobile_maps>cordova platform add android
[Error: The command `android` failed. Make sure you have the latest Android SDK
installed, and the `android` command (inside the tools/ folder) added to your pa
th. Output: ]
Second EDIT:
i also get 'xcopy' is not recognized as an internal or external command even though I've added %SystemRoot%\System32; %SystemRoot%; %SystemRoot%\System32\wbem;%SystemRoot%\System32\WindowsPowerShell\v1.0 . I really don't know what else I can do..
usually this happens when:
1. Java is not installed
2. Java is not exported (no JAVA_HOME env variable) or paths are wrong
3. only 64bit version of Java is installed
to fix install 32bit support on your system.
and (as comments below) relog in Windows to environment changes take effect.
A few days ago I started with Ubuntu for the first time. I want to use Android SDK with Eclipse. I installed everything needed. It only goes wrong when I try to start the Android SDK. There is a proxy server on the internet here, but when I try to insert it at:
Eclipse > Windows > Preference > Android > Launch and then enter the proxy in Default Emulator Options (link: How to set Internet options for Android emulator?)
Each time I start the SDK via Eclipse, I get failed to fetch with... so I found out that I can enter the proxy via the android tool. But when I enter ./android-sdk-linux/tools/android I get some errors:
./android-sdk-linux/tools/android: 84: java: not found
./android-sdk-linux/tools/android 101" java: not found
exec: 114: java: not found
I installed the JDK from oracle. I followed the steps and it did install via the terminal. After that I added it to the PATH(maybe I did it wrong here). I tried it with the JRE too. Eclipse gave an error at the begin about java. But when I installed a copy of the JRE in the Eclipse folder, it didn't give an error anymore.
So I think it's my JDK, but I don't know what I did wrong.
I use Ubuntu 11.10. 64bit via VMware Player.
On Ubuntu I had to go into the /sdk/tools/android and change the java_cmd variable to equal my JAVA_HOME path - then it worked.
[path from comment]
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home//android-sdk-linux/tools:/home//jdk1.6.0_29
Change the path of your JDK in $PATH to /home/<user_name>/jdk1.6.0_29/bin.
I have the same problem, and fixed it. I'm running Debian based Linux Distribution. And my setting $PATH variable environment didn't work even thought I can run java from Konsol.
Here is another better solution.
Linux Ubuntu Android SDK manager not starting up - android:java: not found
Solution which I mean is, make a symbolic link /usr/bin