当安装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)

当前回答

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

其他回答

更新2019 - 10:

正如问题跟踪器中所述,谷歌一直在开发一个新的Android SDK命令行工具版本,该版本运行在当前的jvm(9,10, 11+)上,并且不依赖于已弃用的JAXB EE模块!

您可以在Android Studio内下载和使用新的Android SDK命令行工具,也可以从谷歌服务器手动下载:

Linux SDK工具 Mac OS SDK工具 Windows SDK工具

对于最新版本,请检查repository.xml中的url。

如果您手动解包命令行工具,请注意将它们放在$ANDROID_HOME内的子文件夹中(例如$ANDROID_HOME/cmdline-tools/…)

2021 - 03年更新:

最新的稳定命令行工具可以在google下载网站上找到。这些工具比上面链接的工具更新。

奇怪的是,Java9与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

将所有命令合并为一个命令,以便于参考:

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

对于我的情况,我使用sdkman来管理多个java版本。我将默认java版本设置为13。安装版本8,现在可以正常工作了。

不需要卸载机器上已经安装的其他java版本。无论何时需要,您都可以方便地使用实用程序“update-alternatives”来选择您希望激活的Java运行时。它将自动更新所需的符号链接。

您只需要运行下面的命令并选择您所选择的版本。这是所有!

sudo update-alternatives --config java

在构建flutter应用程序时发生错误,提示android sdk许可证不接受。因此,在接受许可时发生了此错误。

在我的案例中出现这个错误的原因是我没有安装接受许可证所需的sdkmanager命令行工具。

所以,安装命令行工具最简单的方法是:

打开android studio。 打开SDK管理器(参考下图):

在工具栏中选择SDK工具。 勾选选项Android SDK命令行工具(最新) 应用它。 如果使用flutter,则运行flutter doctor -android-licenses,或者您可以继续执行发生错误的进程。