当安装android sdk工具时,会产生以下错误:

java.lang.NoClassDefFoundError: javax / xml /绑定/注释/ XmlSchema

为什么会发生这种情况,如何解决?

调试输出:

$ 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)

当前回答

因为Java 11已经删除了JavaEE,你需要下载一些jar并添加到类路径中:

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

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

然后编辑sdkmanager.bat,以便设置CLASSPATH=…以;%CLASSPATH%结束

将CLASSPATH设置为包含JAXB和JAF:

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

那么sdkmanager.bat就可以工作了。

其他回答

我在跑步时也遇到了同样的问题:

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

我解的是

$ 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

将Java改为使用1.8

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

然后,相同的命令可以正常运行

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

Linux用户(我使用的是Debian发行版,Kali) 以下是我的解决方法。

如果您还没有jdk-8,您可以在oracle的网站上获得它

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

我得到了jdk-8u191-linux-x64.tar.gz

步骤1 -安装Java 在一个合适的位置移动并打开它,像这样

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

您应该得到一个解压缩的文件夹,如jdk1.8.0_191 您可以随后删除tarball以节省空间

步骤2 -设置默认java位置的替代方案

$ 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

步骤3 -选择您的默认选项

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

步骤4 -确认默认java版本

$ java -version

笔记

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. 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 Moving some things around may require root authority. Use sudo wisely. For sdkmanager usage, see official guide: https://developer.android.com/studio/command-line/sdkmanager

更新了我当前的Android SDK,一切都很顺利。

我也遇到了同样的问题,并按照建议安装了openjdk-8-jdk。检查javac -version会得到正确的版本,但是java -version显示的是版本11。

解决方案是:

sudo update-alternatives --config java

然后从菜单中选择版本8。

在我的例子中,我有Java 14,需要Java 8。

我使用的是Arch Linux,并安装了jdk8-openjdk jre8-openjdk https://www.archlinux.org/packages/extra/x86_64/java8-openjdk/

Debian用户https://wiki.debian.org/Java, Fedora用户https://docs.fedoraproject.org/en-US/quick-docs/installing-java/。

在执行以下步骤之前,使用包管理器安装Java 8(或所需版本,在本例中为jdk8-openjdk jre8-openjdk)。


1. 找出我的Java在哪里:

# which java
/usr/bin/java

2. 检查java文件:

我可以看到这里所有的java文件都链接到/usr/lib/jvm/default[一些]。这意味着java命令链接到特定版本的java可执行文件。

# 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.检查default和default-runtime

在这里我可以看到默认版本链接到14(唯一安装版本)。

# 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. 切换默认版本

首先,删除现有的链接到java-14版本的default和default-runtime。

# rm default default-runtime

然后,创建到所需版本(在本例中为java-8)的新链接。

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

策略是使用上面的ln -s链接到所需的软件版本(在本例中是java8)。然后,这些链接链接到java bin目录中的二进制文件(不改变$PATH环境变量!)


或者,您可能希望使用archlinux-java命令更改Java版本,而不是使用更安全的方法:https://wiki.archlinux.org/index.php/Java