How To Fix Java.Lang.NoClassDefFoundError: Javax/xml/bind/annotation/xmlschema When Install Android SDK

When I install android SDK use android SDK manager, it throws an error exception in thread “main” java.lang.noclassdeffounderror: javax/xml/bind/annotation/xmlschema . It cost me a lot of time to investigate. Now I will tell you how to fix it in detail.

$ ./sdkmanager
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.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:73)
  at com.android.sdklib.tool.sdkmanager.SdkManagerCli.main(SdkManagerCli.java:48)
Caused by: java.lang.ClassNotFoundException: javax.xml.bind.annotation.XmlSchema
  at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
  at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
  at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
  ... 5 more

1. Downgrade JDK Version To Fix Exception in thread “main” java.lang.NoClassDefFoundError:javax/xml/bind/annotation/XmlSchema Error.

The reason for this error is because the JDK version that is used is too new. So first you need to find out which Java version are you using.

  1. Open a terminal and run the command $ java -version like below.
    $ java -version
    java version "12.0.1" 2019-04-16
    Java(TM) SE Runtime Environment (build 12.0.1+12)
    Java HotSpot(TM) 64-Bit Server VM (build 12.0.1+12, mixed mode, sharing)
  2. My java version is 12.0.1, this java version will cause this error, so I need to install another java version such as java 8 and then set java 8 as my default java JDK. You can read the article How To Install / Uninstall Multiple Java Versions In Mac OS By Home Brew Or Manually to learn more.
  3. When you install java 8 successfully, run the command ./sdkmanager in terminal again, then the error will disappear.

2. Edit JAVA_OPTS Environment Variable Value To Fix Exception in thread “main” java.lang.NoClassDefFoundError:javax/xml/bind/annotation/XmlSchema Error.

If you do not want to downgrade your JDK version, you can edit the environment variable JAVA_OPTS value like below to fix this error.

  1. For Windows, you can run the below command in a command console to set the system environment variable JAVA_OPTS. Please read the article How To Set Windows Environment Variables.
    setx JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee /M
    
    or
    
    # If the above code does not take effect, then export the java.xml.bind like below code.
    setx JAVA_OPTS=-XX:+IgnoreUnrecognizedVMOptions --add-modules java.xml.bind /M
  2. For Linux or macOS, edit file .bash_rc or .zshrc ( Linux ),  .bash_profile ( macOS ), add below code to export the system environment variable JAVA_OPTS.
    export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
    
    or
    
    # If the above code does not take effect, then export the java.xml.bind like below code.
    export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.xml.bind'

3. Google Released New Android SDK Command-line Tools For JVM 9, 10, 11, And Later.

  1. Google has released new Android SDK Command-line Tools for JVMs 9 and later versions. You can see this on the google issue tracker website.
  2. The new Android SDK Command-line Tools do not depend on JAXB EE modules which have been deprecated.
  3. You can download them in Android Studio to use them.
  4. You can also manually download them to your local machine from Google to use them.
  5. If you manually download them, you should unpack them to a subdirectory of the android_home directory ( set by environment variable $ANDROID_HOME ). For example, you can unpack the new Android SDK Command-line Tools to folder $ANDROID_HOME/command-line-tools.
  6. You can download the new Android SDK Command-line Tools follow these links.  New Android SDK Tools Linux Version , New Android SDK Tools Windows VersionNew Android SDK Tools macOS Version.

6 thoughts on “How To Fix Java.Lang.NoClassDefFoundError: Javax/xml/bind/annotation/xmlschema When Install Android SDK”

  1. Brian Blankenship

    So, for me, it was because JAVA_HOME was not set. So chances are if you are encountering this, sdkmanager probably can’t find your JRE. To fix, try doing the following:

    $ export JAVA_HOME=/path/to/jre
    

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.