Failed to find 'JAVA_HOME' environment variable in Mac

ToniTJK picture ToniTJK · Mar 28, 2018 · Viewed 27k times · Source

I'm new on Mac, so I installed Cordova (Node, Git, Grandle) but I can't execute it in my mobile phone, I mean I can't do "sudo Cordova run android" because it can't find "JAVA_HOME". When I installed I put this variable before... (It is a headache 🤕)

I tried to open the bash_profile but I didn't find it and if I open in terminal it always open a new file.

If I do echo $JAVA_HOME, it is the result:

echo $JAVA_HOME

/usr/libexec/java_home

And this is my error:

sudo cordova run android --target=ZY223T38PM

(node:1369) UnhandledPromiseRejectionWarning: CordovaError: Failed to find 'JAVA_HOME' environment variable. Try setting setting it manually. at /Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/lib/check_reqs.js:185:35 at _rejected (/Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:864:24) at /Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:890:30 at Promise.when (/Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:1142:31) at Promise.promise.promiseDispatch (/Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:808:41) at /Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:624:44 at runSingle (/Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:137:13) at flush (/Users/tonitorres/Dropbox/HardwareHome/hardwareHome/platforms/android/cordova/node_modules/q/q.js:125:13) at _combinedTickCallback (internal/process/next_tick.js:131:7) at process._tickCallback (internal/process/next_tick.js:180:9) (node:1369) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:1369) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Answer

catch23 picture catch23 · Mar 28, 2018

You didn't install JAVA_HOME correctly, therefore it fails.

You have to do it like following:

After installation you have to add installed java to system variable:

Run in terminal for verification:

java -version

The output should be something like:

java version "1.8.0_06-ea"
Java(TM) SE Runtime Environment (build 1.8.0_06-ea-b13)
Java HotSpot(TM) 64-Bit Server VM (build 23.2-b04, mixed mode)

After this check, you can import java to your IDE.