Ionic Build can't find JDK set in JAVA_HOME ev

2019-04-28 18:53发布

问题:

When I installed Java JDK 1.8.x it initially was working, when I rebooted the next day after shutting down over night I get this when I build:

$ ionic build android
Updated the hooks directory to have execute permissions
Running command: d:\installs\nodejs\node.exe d:\htdocs\publicworks\mobile\hooks\after_prepare\010_add_platform_class.js d:/htdocs/publicworks/mobile
add to body class: platform-android
Running command: d:\htdocs\publicworks\mobile\platforms\android\cordova\build.bat
ANDROID_HOME=c:\Users\mtpultz\AppData\Local\Android\android-sdk
JAVA_HOME=C:\Program Files\Java\jdk1.8.0_45\bin
Running: d:\htdocs\publicworks\mobile\platforms\android\gradlew cdvBuildDebug -b d:\htdocs\publicworks\mobile\platforms\android\build.gradle -Dorg.gradle.daemon=true

ERROR: JAVA_HOME is set to an invalid directory: C:\Program Files\Java\jdk1.8.0_45\bin

Please set the JAVA_HOME variable in your environment to match the
location of your Java installation.

d:\htdocs\publicworks\mobile\platforms\android\cordova\node_modules\q\q.js:126
                    throw e;
                          ^
Error code 1 for command: cmd with args: /s /c "d:\htdocs\publicworks\mobile\platforms\android\gradlew cdvBuildDebug -b d:\htdocs\publicworks\mobile\platforms\android\build.gradle -Dorg.gradle.daemon=true"

But Java is most definitely installed in this directory and nowhere else??? In my terminal:

java -version

Produces the proper output for my JDK:

java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

The environment variables are set to:

JAVA_HOME =

C:\Program Files\Java\jdk1.8.0_45\bin

and the path contains:

D:\installs\nodejs;
C:\Users\mtpultz\AppData\Roaming\npm;
D:\installs\apache-ant\bin;
C:\Users\mtpultz\AppData\Local\Android\android-sdk\tools;
C:\Users\mtpultz\AppData\Local\Android\android-sdk\platform-tools;

Any suggestions?

回答1:

JAVA_HOME should be set to the home directory of java and not to bin. So replace this:

C:\Program Files\Java\jdk1.8.0_45\bin

with

C:\Program Files\Java\jdk1.8.0_45

java/bin dir is added to PATH system variable to run java from anywhere.



回答2:

Do one thing. This will Help You

Remove "\bin" from your JAVA_HOME path. Mean give path only till.

C:\Program Files\Java\jdk1.8.0_45

It will work with you.



回答3:

For Linux user:


$sudo gedit $HOME/.bashrc

write at end

#Android Path
export ANDROID_HOME=${HOME}/Android/Sdk
export PATH=${PATH}:${ANDROID_HOME}/platform-tools:${ANDROID_HOME}/tools
export PATH
#JAVA
export JAVA_HOME=/usr/lib/jvm/java-8-oracle/
export JAVA_HOME

$source ~/.bashrc

after that open


$sudo gedit /etc/environment

and add at end

JAVA_HOME="/usr/lib/jvm/java-8-oracle/"

$source /etc/environment



回答4:

after wasting 3 days finally i got some solutions. these are definitely works when you found these errors.

1.Error: JAVA_HOME is set to an invalid directory

JAVA_HOME should be set to the home directory of java and not to bin. So replace this:

C:\Program Files\Java\jdk1.8.0_45\bin with

C:\Program Files\Java\jdk1.8.0_45 java/bin dir is added to PATH system variable to run java from anywhere

  1. Failed to install 'cordova-plugin-whitelist': Error using forcedroid command line

check your environments variables 1. android platforms tools 2. android tools 3. sdk location 4. git location

  1. cordova - Error: Failed to fetch platform android

the only solution is

cordova platform add android --nofetch



回答5:

Make sure you use the Java SE SDK and not the Java EE SDK, that solved the problem for me.



回答6:

Make sure that your JAVA_HOME is having the following path and install JDK SE.

C:\Program Files\Java\jdk1.8.0_131