Android SDK manager throw Exception with Java 9

Aero Wang picture Aero Wang · Nov 17, 2017 · Viewed 13.6k times · Source

Using sdk-tools-darwin-4333796 (26.1.1) with Java 9 when running sdkmanager is throwing :

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: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

Answer

James Wayne picture James Wayne · Nov 17, 2017

Edit sdkmanager file, find DEFAULT_JVM_OPTS and add --add-modules java.xml.bind argument to it.

For instance, if your sdkmanager file has

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

change it to

DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" --add-modules java.xml.bind'

Update: the above works for Java 9 and 10. The java.xml.bind module was removed in Java 11, and will not be found.
https://www.oracle.com/java/technologies/javase/jdk-11-relnote.html#JDK-8190378


Additional Update: Apparently there are two versions of the sdkmanager, a deprecated one that requires Java8 and a new one that works with Java 11. See this answer on StackOverflow