nativescript-cli icon indicating copy to clipboard operation
nativescript-cli copied to clipboard

[OpenJDK] tns platform add android: Error executing command 'javac'.

Open daxim opened this issue 10 years ago • 38 comments

Following the tutorial, step 1-3:

~/hack/nativescript/sample-Groceries$ tns platform add android
Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable.
~/hack/nativescript/sample-Groceries$ echo $JAVA_HOME
/usr/lib64/jvm/java
~/hack/nativescript/sample-Groceries$ javac
Usage: javac <options> <source files>
where possible options include:
  @<filename>                  Read options and filenames from file
  -Akey[=value]                Options to pass to annotation processors
  --add-modules <module>(,<module>)*
        Root modules to resolve in addition to the initial modules, or all modules
        on the module path if <module> is ALL-MODULE-PATH.
  --boot-class-path <path>, -bootclasspath <path>
        Override location of bootstrap class files
  --class-path <path>, -classpath <path>, -cp <path>
        Specify where to find user class files and annotation processors
  -d <directory>               Specify where to place generated class files
  -deprecation
        Output source locations where deprecated APIs are used
  -encoding <encoding>         Specify character encoding used by source files
  -endorseddirs <dirs>         Override location of endorsed standards path
  -extdirs <dirs>              Override location of installed extensions
  -g                           Generate all debugging info
  -g:{[lines, vars, source],[lines, vars, source],[lines, vars, source]}
        Generate only some debugging info
  -g:none                      Generate no debugging info
  -h <directory>
        Specify where to place generated native header files
  --help, -help                Print a synopsis of standard options
  -implicit:{[none, class],[none, class]}
        Specify whether or not to generate class files for implicitly referenced files
  -J<flag>                     Pass <flag> directly to the runtime system
  --limit-modules <module>(,<module>)*
        Limit the universe of observable modules
  --module <module-name>, -m <module-name>
        Compile only the specified module, check timestamps
  --module-path <path>, -p <path>
        Specify where to find application modules
  --module-source-path <module-source-path>
        Specify where to find input source files for multiple modules
  -nowarn                      Generate no warnings
  -parameters
        Generate metadata for reflection on method parameters
  -proc:{[none, only],[none, only]}
        Control whether annotation processing and/or compilation is done.
  -processor <class1>[,<class2>,<class3>...]
        Names of the annotation processors to run; bypasses default discovery process
  --processor-module-path <path>
        Specify a module path where to find annotation processors
  --processor-path <path>, -processorpath <path>
        Specify where to find annotation processors
  -profile <profile>
        Check that API used is available in the specified profile
  --release <release>
        Compile for a specific VM version. Supported targets: 6, 7, 8, 9
  -s <directory>               Specify where to place generated source files
  -source <release>
        Provide source compatibility with specified release
  --source-path <path>, -sourcepath <path>
        Specify where to find input source files
  --system <jdk>|none          Override location of system modules
  -target <release>            Generate class files for specific VM version
  --upgrade-module-path <path>
        Override location of upgradeable modules
  -verbose                     Output messages about what the compiler is doing
  -version                     Version information
  -Werror                      Terminate compilation if warnings occur
  -X                           Print a synopsis of nonstandard options

The message from the tns platform command is useless because it does not say what the error is.

daxim avatar Nov 27 '16 08:11 daxim

@mnovak20 is there a reason why you chose that version of the JDK and not JDK 9?

sainsley123 avatar Sep 27 '17 18:09 sainsley123

So, still not working. Nativscript version: 3.2.1 Javac version: 9 Java version: 9 ANDROID_HOME: /Users/username/Library/Android/sdk JAVA_HOME: /Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home

When I run tns run android --emulator I get

Error executing command 'javac'. Make sure you have installed The Java Development Kit (JDK) and set JAVA_HOME environment variable.

I tried @mnovak20 solution and no luck.

sainsley123 avatar Sep 27 '17 18:09 sainsley123

I originally started with 9, that didn't work so I installed 8, that still didn't work. At that point I had 2 JDKs so I uninstalled both and installed 8 again. That's when tns doctor stopped throwing errors.

It may work fine with JDK9 but it just didn't for me.

mnovak20 avatar Sep 27 '17 18:09 mnovak20

Same goes for me, I was using java-9-openjdk and it was showing the same error, I then installed java-8-oracle (according to the documentation) and that fixed it.

Spatlani avatar Sep 28 '17 05:09 Spatlani

@Spatlani and @mnovak20 that did it. I used jdk-8u144 and reset the JAVA_HOME variable. Thanks all!

sainsley123 avatar Sep 28 '17 11:09 sainsley123

Hi Same here, it does not seem to work with the latest version of the JDK (jdk-9.0.1)

jon301 avatar Oct 22 '17 09:10 jon301

A couple of tips for people running into this (at least for MAC some of these worked). Make sure your using JDK 8, - 9 doesn't work (the docs have been updated recently to reflect that).

Next, if you're getting the JAVA_HOME issues (even though you exported them at some other time), update your bash_profile

nano .bash_profile

// in the file add
export JAVA_HOME=$(/usr/libexec/java_home)
export ANDROID_HOME=/usr/local/share/android-sdk

// then save and exit
ctrl+o  (then Y for yes)
ctrl+x

Hope that helps others, got me able to get through tns doctor.

MarkPieszak avatar Oct 29 '17 02:10 MarkPieszak

You shouldn't need to remove and reinstall JDK versions.

Run javac -version to see which version is being exported. For instance, this will return:

$ javac -version
javac 9.0.1

Use this command to check currently installed versions: $ /usr/libexec/java_home -V

It will show you something like:

$ /usr/libexec/java_home -V
Matching Java Virtual Machines (2):
    9.0.1, x86_64:	"Java SE 9.0.1"	/Library/Java/JavaVirtualMachines/jdk-9.0.1.jdk/Contents/Home
    1.8.0_131, x86_64:	"Java SE 8"	/Library/Java/JavaVirtualMachines/jdk1.8.0_131.jdk/Contents/Home

If you have multiple versions you can select the desired $JAVA_HOME export like so:

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

(if you have multiple versions of 1.8 for instance you should specify the entire version number: 1.8.0_131. If it's unique, 1.8 will do.)

Add that export to ~/.bash_profile to make it default and restart the terminal.

Output of javac -version is now:

$ javac -version
javac 1.8.0_131

Running tns doctor should not show javac error anymore. Hope it helps.

bardolos avatar Nov 15 '17 11:11 bardolos

delete all java version and install java 8. I solved.

seohangyu avatar Nov 23 '17 06:11 seohangyu

i sloved this problem by fixing my .bash_rc JAVA_HOME i used this line export JAVA_HOME= ls -d /usr/lib/jvm/java--openjdk-/ | head -n 1

ahmadmarafa avatar Dec 19 '17 13:12 ahmadmarafa

I fixed the same issue with: export JAVA_HOME=$(/usr/libexec/java_home)

Ihor912 avatar Jan 20 '18 16:01 Ihor912

Hey guys, We have added a check for Java 9 and latest CLI reports it is not supported. I see some of you had issues with setting JAVA_HOME, but after fixing the variable, everything works fine. Can we consider the issue as resolved or you have some suggestions how to improve the checks of tns doctor?

rosen-vladimirov avatar Feb 12 '18 23:02 rosen-vladimirov

I would agree that this should be closed. I resolved this by setting my JAVA_HOME to version 1.8. After reinstalling it of course. :)

sainsley123 avatar Feb 13 '18 01:02 sainsley123

you have some suggestions how to improve the checks of tns doctor?

@rosen-vladimirov, start by not throwing away the error message. It's more than a year later, Java 8, 9, 10 and 11 came out and passed you by.

daxim avatar Feb 13 '18 14:02 daxim

Ubuntu 16.04 Fix

Modification to @MarkPieszak 's comment: https://github.com/NativeScript/nativescript-cli/issues/2265#issuecomment-340232515

vim ~/.profile

Then, at the bottom of the file use i to insert the following:

export ANDROID_HOME=$HOME/Android/Sdk
export PATH=$PATH:$ANDROID_HOME/tools
export JAVA_HOME=$(/usr/lib/jvm/{your_java_version}/bin)
export PATH=$PATH:$JAVA_HOME

Then, save (ctrl+x) and either run the following command:

source ~/.profile

OR restart your terminal.

Hope this helps!

d-pollard avatar Apr 02 '18 23:04 d-pollard

Hey all,

Regarding the support for Java 10, we've created a new issue, you can follow it here: https://github.com/NativeScript/nativescript-cli/issues/3605 (Note - it is planned for the 4.1.0 release)

I'm closing this thread as the initial issue has been resolved.

rosen-vladimirov avatar May 21 '18 08:05 rosen-vladimirov

Yeap, just run this http://i.imgur.com/ytUsVX5.png from https://docs.nativescript.org/angular/start/ns-setup-linux for linux, for windows and macOS please take a look on docs.

miki995 avatar Feb 06 '19 20:02 miki995

Solved this under ubuntu 18.

I had multiple JDK installation

the problem is setting JAVA_HOME with generated dynamic values, for instance:

export JAVA_HOME=$(readlink -f /usr/bin/java | sed "s:bin/java::")
export JAVA_HOME=$(/usr/libexec/java_home)

worked in terminal, but produce the error in tns

when I used a static complete path, it worked: export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64

probably these dynamics values change their values under tns and use another version of the JVM

LucaColombi avatar Jun 26 '19 19:06 LucaColombi