177

When installing the android sdk tools the following error is emitted:

java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

Why is this happening and how can it be fixed?

Debug output:

$ java --version
java 9
Java(TM) SE Runtime Environment (build 9+181)
Java HotSpot(TM) 64-Bit Server VM (build 9+181, mixed mode)
$ brew cask install android-sdk
==> Caveats
We will install android-sdk-tools, platform-tools, and build-tools for you.
You can control android sdk packages via the sdkmanager command.
You may want to add to your profile:
  'export ANDROID_SDK_ROOT=/usr/local/share/android-sdk'

This operation may take up to 10 minutes depending on your internet connection.
Please, be patient.

==> Satisfying dependencies
==> Downloading https://dl.google.com/android/repository/sdk-tools-darwin-3859397.zip
Already downloaded: /Users/tomasnovella/Library/Caches/Homebrew/Cask/android-sdk--3859397,26.0.1.zip
==> Verifying checksum for Cask android-sdk
==> Installing Cask android-sdk
==> Exception in thread "main"
==> java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
==>     at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
==>     at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
==>     at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
==>     at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:117)
==>     at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:93)
==> Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
==>     at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
==>     at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
==>     at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
==>     ... 5 more
Error: Command failed to execute!

==> Failed command:
/usr/local/Caskroom/android-sdk/3859397,26.0.1/tools/bin/sdkmanager tools platform-tools build-tools;26.0.1

==> Standard Output of failed command:


==> Standard Error of failed command:
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
    at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
    at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
    at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
    at com.android.sdklib.tool.SdkManagerCli.main(SdkManagerCli.java:117)
  • I assume yoour question is "What is causing this error?" The answer is that it can't find the javax.xml classes. – Steve Smith Sep 25 '17 at 10:43
  • 29
    Actually this question asks a legit problem with installing the android sdk when having java9 – jontro Oct 19 '17 at 22:37
  • 18
    I has this problem, and I copy/pasted the first line of my error message into google, and this question was the first hit that came up. Not only that, one of the answers below solved it. This should totally not be closed: no matter what the rules of questions are, this question demonstrably fulfills the whole purpose of the site. – Le Mot Juiced Oct 25 '17 at 17:22
  • 1
    Delete anything older than Java 8 from /Library/Java/JavaVirtualMachines – Dimitris Mar 29 '18 at 16:04
  • 3
    The problem occurs with Java 8 and Java 11 on Ubuntu 18.04 using packages openjdk-8-jdk and openjdk-11-jdk. Does anyone know what to install for javax/xml/bind/annotation/XmlSchema, or how to avoid javax/xml/bind/annotation/XmlSchema in the NDK? – jww Oct 15 '19 at 9:41

24 Answers 24

143

I had a similar problem this morning (trying to build for Android using Unity3D). I ended up uninstalling JDK9 and installing Java SE Development Kit 8u144. Hope this helps.

  1. brew cask uninstall java # uninstall java9
  2. brew tap homebrew/cask-versions
  3. brew cask install java8 # install java8
  4. touch ~/.android/repositories.cfg # without this file, error will occur on next step
  5. brew cask install android-sdk
| improve this answer | |
  • 2
    Got the error on windows 10, tried this, still doesn't work. Any tips? – Vedvart1 Jan 22 '18 at 0:15
  • 3
    Great - this worked for me building a Flutter app in Android Studio. Thanks! – Darragh Enright Mar 25 '18 at 16:28
  • 21
    This stopped working, brew cask install java8 does not work. – kdy Apr 25 '19 at 14:23
  • 4
    the correct command seems to be now brew cask install homebrew/cask-versions/java8 – caesarsol Jun 17 '19 at 20:21
  • 23
    brew cask install homebrew/cask-versions/adoptopenjdk8 seems to work – guruz Sep 2 '19 at 9:22
80

To solve this error, you can downgrade your Java version.

Or exports the following option on your terminal:

Linux/MAC:

export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

Windows:

set JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

And to save it permanently you can exports the JAVA_OPTS in your profile file on linux (.zshrc, .bashrc and etc.) or add as environment variable permanently on Windows.


ps. This doesn't work for the Java 11+, which doesn't have Java EE modules. For this option is a good idea, downgrade your Java version or wait for a Flutter update.

Ref: JDK 11: End of the road for Java EE modules

| improve this answer | |
  • 6
    This worked for me in Ubuntu 18.04 w/ OpenJDK 10.0.2 – user1243584 Aug 24 '18 at 14:58
  • 3
    Worked for me on macOS Mojave (10.14.2) with Java version "10.0.1" 2018-04-17. – Subfuzion Dec 28 '18 at 11:44
  • 4
    Created a new error for me on macOS Mojave (10.14.2): Error occurred during initialization of boot layer java.lang.module.FindException: Module java.se.ee not found – Randy Jan 11 '19 at 0:28
  • 2
    I also tried 1.8.0_191, but am still getting: Error: Could not find or load main class java.se.ee – Randy Jan 11 '19 at 1:41
  • 1
    This option is not needed for the Java 8 and doesn't work for the Java 11, which doesn't have Java EE modules. – valdeci Jan 11 '19 at 1:42
66
set JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee

This fixed the problem on Windows for me.

Source 1, source 2

| improve this answer | |
  • 2
    Also used this on ubuntu 16.04 (using export not set) and it resolved the problem. – Benjamin Conlan Jun 17 '18 at 10:18
  • Genius. Thanks so much. Thought I was gonna have to change my JVM – flakes Jul 15 '18 at 14:38
  • 1
    Worked like a charm on Windows 10 with Java 10 – pabz Jul 29 '18 at 4:06
  • 4
    Unfortunately that seems to have been removed entirely in Java 11. – Alastair Maw Nov 3 '18 at 22:49
  • Worked doing export JAVA_OPTS = '-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee' on Ubuntu 18.04 – e2-e4 Jan 20 '19 at 11:56
26

If you don't want to change your Java version (I don't), you can temporarily change the version in your shell:

First run

/usr/libexec/java_home -V

Then pick a major version if you have it installed, otherwise install it first:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

Now you can run sdkmanager.

| improve this answer | |
  • 2
    $ /usr/libexec/java_home -V gives bash: /usr/libexec/java_home: No such file or directory. apt-file search /usr/libexec/java_home gives exactly nothing. – Tino Oct 22 '18 at 10:09
  • Do you have $JAVA_HOME set correctly? Run: echo "export JAVA_HOME=`/usr/libexec/java_home`" >> ~/.bashrc (or profile if you use profile) - reload: . ~/.bashrc – Sarah A Oct 22 '18 at 16:10
  • There is no /usr/libexec either. Are you on MacOS? (I am on Ubuntu/Linux and Ubuntu/Windows) – Tino Oct 22 '18 at 18:41
  • I am using mac. where's your Java_home? – Sarah A Oct 22 '18 at 22:02
21

On Mac/Linux use the following command:

export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

Works for both JDK 9 and 10, without patching any script (sdkmanager, avdmanager).

For Java 11 see: https://stackoverflow.com/a/51644855/798165

| improve this answer | |
  • 1
    thanks! it solves the problem when you have installed jdk 9 – Jorge Valvert Oct 9 '18 at 5:38
15

I found two answers that worked for me, without having to uninstall JDK 10 (or 9), which I need for create-react-app. Both JDK 9 and 10 are incompatible with android-sdk !


Siu Ching Pong -Asuka Kenji- suggests modifying the sdkmanager script, replacing this line:

DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME"'  

with:

DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

Note that this mod will be overwritten when updating sdkmanager.
Check out his post, and the one he links to, for more details.
This solution was also one of the solutions mentioned in this github issues thread.


German's post indicates the source of the conflict, and presents fix that will not not be overwritten by updates.
He suggests renaming /Library/Java/JavaVirtualMachines/Info.plist as a means of obscuring it from the script that looks for the highest version of Java that resides on your system. In this way, JDK 8 is returned as the default.
Referring to JDK 10 explicitly, or by setting it to $JAVA_HOME, you can use JDK 10 , instead of the default, whenever needed.
Details are in his post.

| improve this answer | |
  • 1
    Worked perfectly, best solution IMHO as it doesn't force you to downgrade java – 4levels May 20 '18 at 10:15
  • Somehow, after experimenting a lot with ao. Android Studio, this issue disappeared and the new error was Could not find or load main class java.se.ee - reverting the DEFAULT_JVM_OPTS fixed this and the previous issue went away.. – 4levels May 30 '18 at 10:44
  • 3
    Unfortunately, this doesn't work now. The result is: Error occurred during initialization of boot layer java.lang.module.FindException: Module java.se.ee not found – Oscar Jan 28 '19 at 22:20
  • 1
    Upvoted stackoverflow.com/a/49630166/5411817 for having the correct solution for keeping jdk-11 and jdk1.8 installed without downgrading – harrisjb Apr 29 '19 at 3:46
15

You need to add the following to your Profile (Works on MacOS):

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

No need to patch anything.

| improve this answer | |
15

Simply install JDK version 8 and select it as the default using:

sudo update-alternatives --config java
| improve this answer | |
12

Update 2019-10:

As stated in the issue tracker, Google has been working on a new Android SDK Command-line Tools release that runs on current JVMs (9, 10, 11+) and does not depend on deprecated JAXB EE modules!

You can download and use the new Android SDK Command-line Tools inside Android Studio or by manually downloading them from the Google servers:

For the latest versions check the URLs inside the repository.xml.

If you manually unpack the command line tools, take care of placing them in a subfolder inside your $ANDROID_HOME (e.g. $ANDROID_HOME/cmdline-tools/...).

| improve this answer | |
  • 1
    When using Java 9+, this should be the answer from now on! – Luiggi Mendoza Jul 3 at 21:28
  • You should probably use the new command-line tools regardless of Java 8 oder 9+ ;) – G00fY 3 hours ago
9

Strangely Java9 is not compatible with android-sdk

$ avdmanager
Exception in thread "main" java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema
    at com.android.repository.api.SchemaModule$SchemaModuleVersion.<init>(SchemaModule.java:156)
    at com.android.repository.api.SchemaModule.<init>(SchemaModule.java:75)
    at com.android.sdklib.repository.AndroidSdkHandler.<clinit>(AndroidSdkHandler.java:81)
    at com.android.sdklib.tool.AvdManagerCli.run(AvdManagerCli.java:213)
    at com.android.sdklib.tool.AvdManagerCli.main(AvdManagerCli.java:200)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:582)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:185)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:496)
    ... 5 more

Combined all commands into one for easy reference:

$ sudo rm -fr /Library/Java/JavaVirtualMachines/jdk-9*.jdk/
$ sudo rm -fr /Library/Internet\ Plug-Ins/JavaAppletPlugin.plugin
$ sudo rm -fr /Library/PreferencePanes/JavaControlPanel.prefPane

$ /usr/libexec/java_home -V
Unable to find any JVMs matching version "(null)".
Matching Java Virtual Machines (0):
Default Java Virtual Machines (0):
No Java runtime present, try --request to install

$ brew tap caskroom/versions
$ brew cask install java8
$ touch ~/.android/repositories.cfg
$ brew cask install android-sdk
$ echo 'export ANDROID_SDK_ROOT="/usr/local/share/android-sdk"' >> ~/.bash_profile
$ java -version
java version "1.8.0_162"
Java(TM) SE Runtime Environment (build 1.8.0_162-b12)
Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
$ avdmanager

Usage:
      avdmanager [global options] [action] [action options]
      Global options:
  -s --silent     : Silent mode, shows errors only.
  -v --verbose    : Verbose mode, shows errors, warnings and all messages.
     --clear-cache: Clear the SDK Manager repository manifest cache.
  -h --help       : Help on a specific command.

Valid actions are composed of a verb and an optional direct object:
-   list              : Lists existing targets or virtual devices.
-   list avd          : Lists existing Android Virtual Devices.
-   list target       : Lists existing targets.
-   list device       : Lists existing devices.
- create avd          : Creates a new Android Virtual Device.
-   move avd          : Moves or renames an Android Virtual Device.
- delete avd          : Deletes an Android Virtual Device.
| improve this answer | |
8

Since Java 11 has removed JavaEE you'll need to download some jars and add to the classpath:

JAXB: https://javaee.github.io/jaxb-v2/

JAF: https://www.oracle.com/technetwork/articles/java/index-135046.html

Then edit sdkmanager.bat so that set CLASSPATH=... ends with ;%CLASSPATH%

Set CLASSPATH to include JAXB and JAF:

set CLASSPATH=jaxb-core.jar;jaxb-impl.jar;jaxb-api.jar;activation.jar

Then sdkmanager.bat will work.

| improve this answer | |
  • It works, but I get this warning: WARNING: Illegal reflective access by com.sun.xml.bind.v2.runtime.reflect.opt.Injector (file:/F:/android_sdk/java/jaxb-ri/lib/jaxb-impl.jar) to method java.lang.ClassLoader.defineClass(java.lang.String,byte[],int,int) – AndyMc Dec 10 '18 at 10:41
  • Ya, looks like Java is starting to warn against bad programming practice of using reflection to bypass class security, which JAXB uses (Oracle breaking their own rules) ;) – Peter Quiring Dec 10 '18 at 15:00
5

I faced the same problem. Though I am a little bit backdated developer (Still using windows to develop :P)

To solve this issue on windows :

STEP 1: Install jdk 8 if it wasn't installed (jdk 9 or 11 doesn't work but you may have them installed for using in other dev uses).

Very simple using Chocolatey:

choco install jdk8

(If installed using Chocolatey, skip steps 2 and 3)

STEP 2: Go to the Environment variables settings and set JAVA_HOME TO jdk 8's installation directory.

JAVA_HOME

STEP 3: Go to path variable and add bin directory of jdk 8 and move it to top.

Path_varriable

STEP 4: Close any open terminal sessions and restart a new session

OPTIONAL STEP 5: Depending on your objective in the terminal run (may need to add sdkmanager to path or just navigate to the directory):

sdkmanager --update

That's all! :O Enjoy fluttering! :D

| improve this answer | |
  • 1
    This along with the top answer worked for me on windows 10! – mauriii Apr 8 at 10:11
  • THANKSSSSSSSSSSSSSS – Raghav Joshi Jul 10 at 18:57
  • Works! I just want to add, that if you are using VS code, you have to restart the program after this change – Antonio Reyes Jul 24 at 6:57
4

I ran into same issue when running:

$ /Users/<username>/Library/Android/sdk/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"_

I solved it as

$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk-11.0.1.jdk/Contents/Home    

$ ls /Library/Java/JavaVirtualMachines/

jdk-11.0.1.jdk      
jdk1.8.0_202.jdk

Change Java to use 1.8

$ export JAVA_HOME='/Library/Java/JavaVirtualMachines/jdk1.8.0_202.jdk/Contents/Home'

Then the same command runs fine

$ /Users/<username>/Library/Android/sdk/tools/bin/sdkmanager "platforms;android-28" "build-tools;28.0.3"
| improve this answer | |
  • This is the best case. If you already export JAVA_OPTS. Please remove it first before you do the steps from this answer. – Sword Jason Apr 9 '19 at 3:08
2

For windows machine uninstall the JDK if its more than 1.8.172. Install JDK 1.8.172

I was facing the same issue in windows 10 with java 10. I uninstalled the java 10 and installed java8 its working fine for me now :)

| improve this answer | |
2

For Linux users (I'm using a Debian Distro, Kali) Here's how I resolved mine.

If you don't already have jdk-8, you want to get it at oracle's site

https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

I got the jdk-8u191-linux-x64.tar.gz

Step 1 - Installing Java Move and unpack it at a suitable location like so

$ mv jdk-8u191-linux-x64.tar.gz /suitablelocation/
$ tar -xzvf /suitablelocation/jdk-8u191-linux-x64.tar.gz

You should get an unzipped folder like jdk1.8.0_191 You can delete the tarball afterwards to conserve space

Step 2 - Setting up alternatives to the default java location

$ update-alternatives --install /usr/bin/java java /suitablelocation/jdk1.8.0_191/bin/java 1
$ update-alternatives --install /usr/bin/javac javac /suitablelocation/jdk1.8.0_191/bin/javac 1

Step 3 - Selecting your alternatives as default

$ update-alternatives --set java /suitablelocation/jdk1.8.0_191/bin/java
$ update-alternatives --set javac /suitablelocation/jdk1.8.0_191/bin/javac

Step 4 - Confirming default java version

$ java -version

Notes

  1. In the original article here: https://forums.kali.org/showthread.php?41-Installing-Java-on-Kali-Linux, the default plugin for mozilla was also set. I assume we don't really need the plugins as we're simply trying to develop for android.
  2. As in @spassvogel's answer, you should also place a @repositories.cfg file in your ~/.android directory as this is needed to update the tools repo lists
  3. Moving some things around may require root authority. Use sudo wisely.
  4. For sdkmanager usage, see official guide: https://developer.android.com/studio/command-line/sdkmanager
| improve this answer | |
1

Run java -version and javac -version commands in a command line to make sure that they come from the same JDK (eg: version 1.8.0_181)

If not, you have to modify PATH variable so that it only points to a single JDK. If you are not sure how to, just uninstall all other Java instances except for Java 8 (Add/Remove Programs in Windows). As for today, both Unity and Android recommends that you use JDK 8.

With Java 8, it is not necessary to export java.se.ee module as shown in some of the other answers. You may also remove any JAVA_OPTS or other environment variables that you have set.

| improve this answer | |
1

I had recently solved this problem by uninstalling the higher version of JDK and installing JDK 8. After installing the JDK you need to give the path. Then you need to open command prompt in "C:\Users\Milan Adhikari\AppData\Local\Android\Sdk\tools" and run "sdkmanager --update" which will update your sdk and then you need to run "flutter doctor --android-licenses" in cmd and accept all the licenses.
Now your problem should be solved.

| improve this answer | |
1

In my case, I need both JDK 8 (trying to use the AVD and SDK manager in Qt under ubuntu) and 11 for different tools. Removing version 11 is not an option.

The 'JAVA_OPTS' solutions did not do anything. I don't really like the export JAVA_HOME, as it might force you do launch whatever tool calls these utils from the same shell (like Qt), or force you to make this permanent, which is not convenient.

So for me the solution is quite simple. Add something like this in the second line of ~/Android/tools/bin/sdkmanager and ~/Android/tools/bin/avdmanager:

JAVA_HOME="/usr/lib/jvm/java-8-openjdk-amd64"

(or whatever the path is to your rev 8 jdk).

With this, these command line tools work in a stand alone mode, they work also when called by other tools such as Qt, and jdk 11 is still the system default for others. No need to mix libs etc...

The only downside is that any update to these command line tools will erase these modifications, which you will have to put back in.

| improve this answer | |
0

Downgrade your java version.Whatever system or ide.

Make sure java version is not higher than 8

In my case.I change the ide java verion.This solves my issue. enter image description here

| improve this answer | |
0

When your Android stuio/jre uses a differ version of java, you will receive this error. to solve it, just set Android studio/jre to your JAVA_HOME. and uninstall your own java.

| improve this answer | |
0

For me i install java version 8 and just select the java version in "JDK location": enter image description here

| improve this answer | |
0

For my circumstance, I'm using sdkman to manage multiple java versions. I set default java version to 13. Install version 8 and now it's working fine.

| improve this answer | |
0

Best way is to use below command

   $ wget https://dl.google.com/android/repository/platform-tools-latest-linux.zip
   $ unzip \platform-tools-latest-linux.zip
   $ sudo cp platform-tools/adb /usr/bin/adb
   $ sudo cp platform-tools/fastboot /usr/bin/fastboot

Now run adb version to verify it’s been updated.

| improve this answer | |
0

In my case, I have 14 version and need 8.

I'm in a Arch Linux and installed java8 jdk8-opendjdk https://www.archlinux.org/packages/extra/x86_64/java8-openjdk/

For Debian users https://wiki.debian.org/Java, or Fedora https://docs.fedoraproject.org/en-US/quick-docs/installing-java/.

Install java8 (or desired version) using your package manager before doing bellow steps.

1. Figuring out where is my java:

# which java
/usr/bin/java

2. Checking java files:

I can see all java files here are links to /usr/lib/jvm/default[something]. This means that the java command is linked to some specific version java executable.

# ls -l /usr/bin/java*
lrwxrwxrwx 1 root root 37 May 16 06:30 /usr/bin/java -> /usr/lib/jvm/default-runtime/bin/java
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javac -> /usr/lib/jvm/default/bin/javac
lrwxrwxrwx 1 root root 32 May 16 06:30 /usr/bin/javadoc -> /usr/lib/jvm/default/bin/javadoc
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javah -> /usr/lib/jvm/default/bin/javah
lrwxrwxrwx 1 root root 30 May 16 06:30 /usr/bin/javap -> /usr/lib/jvm/default/bin/javap

3. Checking what is default and default-runtime

Here I could see the default version was 14 (unique installed version).

# cd /usr/lib/jvm
# ls -l
lrwxrwxrwx 1 root root   14 Aug  8 20:44 default -> java-14-openjdk
lrwxrwxrwx 1 root root   14 Aug  8 20:44 default-runtime -> java-14-openjdk
drwxr-xr-x 7 root root 4096 Jul 19 22:38 java-14-openjdk
drwxr-xr-x 6 root root 4096 Aug  8 20:42 java-8-openjdk

4. Switching the default version

First remove the existing links to 14 version.

# rm default default-runtime

After this, create new links to the desired version (here 8).

# link -s java-8-openjdk default
# link -s java-8-openjdk default-runtime

The strategy is make a link from desired version of software (java in case) to some default named link file (ln -s above). Then, this default link is linked to command name inside default bin directory (within $PATH environment variable also!)

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.