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

当前回答

我最近通过卸载更高版本的JDK并安装JDK 8解决了这个问题。安装JDK后,您需要给出路径。然后你需要在“C:\Users\Milan Adhikari\AppData\Local\Android\Sdk\tools”中打开命令提示符,并运行“sdkmanager—update”,这将更新你的Sdk,然后你需要在cmd中运行“flutter doctor—Android -licenses”并接受所有许可证。 现在你的问题应该解决了。

其他回答

TLDR;试着在OSX上设置JAVA_HOME工作得很好

export JAVA_HOME=/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home

从AdoptOpenJDK安装JDKs 8 (LTS):

# brew tap adoptopenjdk/openjdk

brew cask install adoptopenjdk/openjdk/adoptopenjdk8

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

在命令行中运行java -version和javac -version命令,确保它们来自同一个JDK(例如:version 1.8.0_181)

如果不是,则必须修改PATH变量,使其只指向单个JDK。如果您不确定如何卸载,只需卸载除Java 8 (Windows中的添加/删除程序)之外的所有其他Java实例。目前,Unity和Android都建议你使用JDK 8。

使用Java 8,不需要导出Java .se.ee模块,如其他一些答案所示。您还可以删除已设置的任何JAVA_OPTS或其他环境变量。

降低java版本。无论系统或ide。

确保java版本不高于8

对我来说。我改变ide java版本。这解决了我的问题。

对于windows机器,如果JDK大于1.8.172,请卸载JDK。 安装JDK 1.8.172

我在windows 10和java 10中也遇到了同样的问题。我卸载了java 10,安装了java8,它现在对我来说工作很好:)