安装 android-sdk 失败:“java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46402772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 02:20:57  来源:igfitidea点击:

Failed to install android-sdk: "java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema"

javaandroidjava-9

提问by Novellizator

When installing the android sdk tools the following error is emitted:

安装 android sdk 工具时,会发出以下错误:

java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

java.lang.NoClassDefFoundError: javax/xml/bind/annotation/XmlSchema

Why is this happening and how can it be fixed?

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

Debug output:

调试输出:

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

采纳答案by spassvogel

I had a similar problem this morning (trying to build for Android using Unity3D). I ended up uninstalling JDK9 and installing Java SE Development Kit 8u144. Hope this helps.

今天早上我遇到了类似的问题(尝试使用 Unity3D 为 Android 构建)。我最终卸载了 JDK9 并安装了Java SE Development Kit 8u144。希望这可以帮助。

  1. brew cask uninstall java# uninstall java9
  2. brew tap homebrew/cask-versions
  3. brew cask install java8# install java8
  4. touch ~/.android/repositories.cfg# without this file, error will occur on next step
  5. brew cask install android-sdk
  1. brew cask uninstall java#卸载java9
  2. brew tap homebrew/cask-versions
  3. brew cask install java8#安装java8
  4. touch ~/.android/repositories.cfg# 没有这个文件,下一步会出错
  5. brew cask install android-sdk

回答by Well Smith

Strangely Java9 is not compatible with android-sdk

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

Combined all commands into one for easy reference:

将所有命令合二为一,方便参考:

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

回答by SherylHohman

I found two answers that worked for me, without having to uninstall JDK 10 (or 9), which I need for create-react-app. Both JDK 9 and 10 are incompatible with android-sdk !

我找到了两个对我有用的答案,而无需卸载 JDK 10(或 9),这是我需要的create-react-app. JDK 9 和 10 都与 android-sdk 不兼容!



Siu Ching Pong -Asuka Kenji-suggests modifying the sdkmanagerscript, replacing this line:

Siu Ching Pong -Asuka Kenji-建议修改sdkmanager脚本,替换这一行:

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

with:

和:

DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'

Note that this mod will be overwritten when updating sdkmanager.
Check out his post, and the one he links to, for more details.
This solution was also one of the solutions mentioned in this github issues thread.

请注意,更新 sdkmanager 时将覆盖此 mod。
查看他的帖子,以及他链接到的帖子,了解更多详情。
此解决方案也是此github 问题线程中提到的解决方案之一。



German's postindicates the source of the conflict, and presents fix that will not not be overwritten by updates.
He suggests renaming /Library/Java/JavaVirtualMachines/Info.plistas a means of obscuring it from the script that looks for the highest version of Java that resides on your system. In this way, JDK 8 is returned as the default.
Referring to JDK 10 explicitly, or by setting it to $JAVA_HOME, you can use JDK 10 , instead of the default, whenever needed.
Details are in his post.

German 的帖子指出了冲突的根源,并提供了不会被更新覆盖的修复。
他建议将重命名/Library/Java/JavaVirtualMachines/Info.plist作为一种从寻找驻留在系统上的 Java 最高版本的脚本中隐藏它的方法。这样,JDK 8 作为默认值返回。
明确引用 JDK 10 或将其设置为$JAVA_HOME,您可以在需要时使用 JDK 10 而不是默认值。
详情在他的帖子里。

回答by Tim Jen?en

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

This fixed the problem on Windows for me.

这为我解决了 Windows 上的问题。

Source 1, source 2

来源 1,来源 2

回答by Jagan

For windows machine uninstall the JDK if its more than 1.8.172. Install JDK 1.8.172

对于 Windows 机器,如果它超过 1.8.172,请卸载 JDK。安装 JDK 1.8.172

I was facing the same issue in windows 10 with java 10. I uninstalled the java 10 and installed java8 its working fine for me now :)

我在使用 java 10 的 windows 10 中遇到了同样的问题。我卸载了 java 10 并安装了 java8,它现在对我来说工作正常:)

回答by oori

On Mac/Linux use the following command:

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

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

Works for both JDK 9 and 10, withoutpatching any script (sdkmanager, avdmanager).

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

For Java 11 see: https://stackoverflow.com/a/51644855/798165

对于 Java 11,请参阅:https: //stackoverflow.com/a/51644855/798165

回答by valdeci

To solve this error, you can downgrade your Java version.

要解决此错误,您可以降级 Java 版本。

Or exports the following option on your terminal:

或者在您的终端上导出以下选项:

Linux/MAC:

Linux/MAC:

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

Windows:

窗户

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

And to save it permanently you can exports the JAVA_OPTSin your profile file on linux (.zshrc, .bashrcand etc.) or add as environment variable permanently on Windows.

并永久保存它,你可以出口JAVA_OPTS在Linux(您的配置文件.zshrc.bashrc等)或永久添加一个环境变量在Windows上。



ps.This doesn't work for the Java 11+, which doesn't have Java EE modules. For this option is a good idea, downgrade your Java version or wait for a Flutter update.

附:这不适用于没有 Java EE 模块的 Java 11+。对于此选项是个好主意,请降级您的 Java 版本或等待 Flutter更新

Ref: JDK 11: End of the road for Java EE modules

参考:JDK 11:Java EE 模块之路的尽头

回答by Gayan Weerakutti

Run java -versionand javac -versioncommands in a command line to make sure that they come from the same JDK (eg: version 1.8.0_181)

在命令行中运行java -versionjavac -version命令以确保它们来自同一个 JDK(例如:版本 1.8.0_181)

If not, you have to modify PATHvariable so that it only points to a single JDK. If you are not sure how to, just uninstall all other Java instances except for Java 8 (Add/Remove Programs in Windows). As for today, both Unity and Android recommends that you use JDK 8.

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

With Java 8, it is not necessary to export java.se.eemodule as shown in some of the other answers. You may also remove any JAVA_OPTSor other environment variablesthat you have set.

使用 Java 8,没有必要java.se.ee像其他一些答案中所示那样导出模块。您还可以删除您设置的任何JAVA_OPTS或其他环境变量

回答by Sarah A

If you don't want to change your Java version (I don't), you can temporarily change the version in your shell:

如果你不想改变你的 Java 版本(我不想),你可以在你的 shell 中临时改变版本:

First run

第一次运行

/usr/libexec/java_home -V

Then pick a major version if you have it installed, otherwise install it first:

如果已安装,则选择一个主要版本,否则请先安装:

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

Now you can run sdkmanager.

现在您可以运行 sdkmanager。

回答by Peter Quiring

Since Java 11 has removed JavaEE you'll need to download some jars and add to the classpath:

由于 Java 11 已删除 JavaEE,因此您需要下载一些 jar 并将其添加到类路径中:

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

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

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

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

Then edit sdkmanager.bat so that set CLASSPATH=...ends with ;%CLASSPATH%

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

Set CLASSPATH to include JAXB and JAF:

设置 CLASSPATH 以包含 JAXB 和 JAF:

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

Then sdkmanager.bat will work.

然后 sdkmanager.bat 将起作用。