当安装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版本(我不想),你可以暂时改变你的shell版本:

第一次运行

/usr/libexec/java_home -V

然后选择一个主版本,如果你安装了它,否则先安装它:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`

现在可以运行sdkmanager了。

其他回答

您需要将以下内容添加到您的配置文件(适用于MacOS):

export JAVA_HOME= ' /usr/libexec/ JAVA_HOME -v 1.8 '

不需要打补丁。

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

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

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

在Mac/Linux上使用以下命令:

export JAVA_OPTS='-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

适用于JDK 9和10,无需修补任何脚本(sdkmanager, avdmanager)。

Java 11参见:https://stackoverflow.com/a/51644855/798165

因为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就可以工作了。