Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

On Windows 8.1 "javac -version" works but Cordova doesn't recognize it

I'm trying to set up Cordova. When I run cordova build android I receive the following error:

(node:6816) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Failed to run "javac -version", make sure that you have a JDK installed.
You can get it from: http://www.oracle.com/technetwork/java/javase/downloads.
Your JAVA_HOME is invalid: C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin

However I can run javac -version just fine.

C:\>javac -version
javac 1.8.0_161

I've tried to set JAVA_HOME in various ways: pointing to the root; pointing to the bin folder; set it as a User variable and also a System one; add %JAVA_HOME%\bin to Path (both for User and System) and so on.

I've checked several articles about this issue, and they say that if I'm able to use javac -version then that's supposed to indicate that my environmental variables are set correctly. Even if that's the case, Cordova still doesn't seem to recognize it.

Update

Following Stephen C's instructions, I've reset my variables, yet my error still persists. As you can see in the image I can call javac just fine, and both JAVA_HOME and Path seem to be set in the right way. (Note that actually it is %JAVA_HOME%\bin and not C:\Program Files\Java\jdk1.8.0_161\bin in the Path.)

enter image description here

like image 209
Letokteren Avatar asked Mar 26 '18 22:03

Letokteren


1 Answers

I think you are confusing JAVA_HOME, PATH and CLASSPATH

  • The JAVA_HOME variable should be set to the name of a single directory. Not multiple directories with separators. The JAVA_HOME directory is the top directory of your JDK or JRE installation. Not the "bin" directory.

  • The PATH and CLASSPATH variables consist of a list of paths. For PATH the paths are pathnames for directories. For CLASSPATH the paths are pathnames for directories or JAR / ZIP files (or a particular kind of wildcard).

  • The PATH is where the shell searches for commands if you use a command name that is a simple name; e.g. java or javac. `

  • The CLASSPATH is one of the ways that you can tell Java tools to look cor compiled classes to load, compile against, etcetera.

Your error message says this:

Your JAVA_HOME is invalid:

C:\Program Files\Java\jdk1.8.0_161;C:\Program Files\Java\jdk1.8.0_161\bin

This is wrong for two reasons:

  1. JAVA_HOME should not be a path
  2. C:\Program Files\Java\jdk1.8.0_161\bin is not a Java home. The Java home is C:\Program Files\Java\jdk1.8.0_161 .... probably.

The other thing you may have gotten wrong is that changes to environment variables do not necessarily propagate. For example, if you start a shell and then change an environment variable via the Windows GUI, the change may not propagate to the shell. You may need to exit the shell and restart it to pick up the new value.

If you are unsure, you can run (for example) echo %PATH% to see the current value of PATH in the current shell or script.

UPDATE

According to http://cordova.apache.org/docs/en/7.x/guide/platforms/android/index.html, you need to:

  • install Android Studio
  • set the ANDROID_HOME environment variable to the location of your Android SDK installation.

It is also recommended that you add the Android SDK's tools, tools/bin, and platform-tools directories to your PATH.

like image 56
Stephen C Avatar answered Oct 11 '22 22:10

Stephen C