Jotaro
Jotaro

Reputation: 93

Failed to find 'JAVA_HOME' environment variable. Try setting it manually

I'm trying to run a sample app on my android device with the following command :

sudo ionic cordova run android --device

I'm getting this error :

Failed to find 'JAVA_HOME' environment variable. Try setting it manually.
[ERROR] An error occurred while running subprocess cordova.

        cordova build android --device exited with exit code 1.

        Re-running this command with the --verbose flag may provide more information.

I already set the JAVA_HOME environment variable but that doesn't work .

~/Ionic/helloWorld$ echo $JAVA_HOME 
/usr/lib/jvm/java-12-openjdk-amd64

Any recommendations ?

Upvotes: 7

Views: 47223

Answers (5)

A S M Sayem
A S M Sayem

Reputation: 2070

Your $JAVA_HOME is pointing to the correct location. But the path should have $JAVA_HOME/bin directory and not $JAVA_HOME itself.

JAVA_HOME="/opt/jdk1.12.0"
export JAVA_HOME
PATH="$PATH:$JAVA_HOME/bin"

You should consider using the Oracle Java PPA instead. It usually does more than what a manual installation would do. You don't have to worry about setting up the environment variables either. That's what most people use.

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer

Try running java -version and javac -version to verify that the path is set.

Hope it helps.

Upvotes: 6

Jayprakash Dubey
Jayprakash Dubey

Reputation: 36447

Step 1:

You’ve Java SDK 12 version which is not set into JAVA_HOME environment. You can set this using below command:

JAVA_HOME="/opt/jdk1.12.0"
export JAVA_HOME
PATH="$PATH:$JAVA_HOME/bin"

Tip: You can search for list of JDK installed on your machine using: cmd + shift + G and type in /Library/Java/JavaVirtualMachines

Step 2:

If you get an error native-run was not found on your PATH then you need to install globally:

npm i -g native-run

Screenshot-1

Step-3:

While running above command if you get permission denied error then run using super admin privilege:

sudo npm i -g native-run

Screenshot-2

Step-4:

Now again when you run sudo ionic cordova run android --device command you may get error regarding gradle. You can fix this using below command in terminal window:

brew install gradle

Tip: It took me around 10 minutes to install gradle (first time user).

Screenshot-3

Step-5:

Now sudo ionic cordova run android --device command should launch you app on android device without any error.

Upvotes: 0

The only way I've gotten it to work is by declaring the variables in the command itself.

Try it like this:

sudo JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64 ANDROID_SDK_ROOT=/usr/lib/Android/Sdk/ cordova requirements android --verbose

Upvotes: 1

user3486626
user3486626

Reputation: 166

I have debian 10 and i solve this problem installing openjdk 1.8. And then you have to run:

sudo update-alternatives --config java

sudo update-alternatives --config javac

You don't need to set JAVA_HOME because with update-alternatives java and javac are linked to bin.

Upvotes: 2

Swayangjit
Swayangjit

Reputation: 1871

You have installed java OpenJDK you should install the Java SE. You can find your system-specific JDK here.

Your JAVA_HOME should look like following

/usr/lib/jvm/java-8-oracle (I have java 8 , you can have any version)

Upvotes: 1

Related Questions