无法使用 Java 9 运行 sdkmanager --list

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47150410/
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:34:19  来源:igfitidea点击:

Failed to run sdkmanager --list with Java 9

javaandroidandroid-sdk-toolsjava-9android-sdk-manager

提问by Siu Ching Pong -Asuka Kenji-

I downloaded and installed:

我下载并安装了:

  • JDK (jdk-9.0.1_osx-x64_bin.dmg) from Oracle here
  • Android SDK (sdk-tools-darwin-3859397.zip) from Google here.
  • JDK ( jdk-9.0.1_osx-x64_bin.dmg) 来自 Oracle这里
  • Android SDK( sdk-tools-darwin-3859397.zip) 来自谷歌这里

After configuring the PATHvariable, I tried running sdkmanager, which replaced the androidcommand for managing SDK components. However, it failed as shown here:

配置完PATH变量后,我尝试运行sdkmanager,它取代了android管理SDK组件的命令。但是,它失败了,如下所示:

$ sdkmanager --list
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

Here is the Java version:

这是Java版本:

$ java -version
java version "9.0.1"
Java(TM) SE Runtime Environment (build 9.0.1+11)
Java HotSpot(TM) 64-Bit Server VM (build 9.0.1+11, mixed mode)

Does anyone know how to fix it without going back to Java 8?

有谁知道如何在不回到 Java 8 的情况下修复它?

Related Questions

相关问题

  • 安装android-sdk失败
    • 这个帖子问了一个类似的问题。但是,该帖子已关闭,唯一的答案是建议返回 Java 8。

采纳答案by Siu Ching Pong -Asuka Kenji-

With the help of this answer, I successfully solved the problem.

这个答案的帮助下,我成功地解决了这个问题。

We are going to apply a fix in sdkmanager. It is a shell script. It is located at $android_sdk/tools/bin, where $android_sdkis where you unzipped the Android SDK.

我们将在sdkmanager. 它是一个shell脚本。它位于$android_sdk/tools/bin$android_sdk您解压 Android SDK 的位置。

  1. Open sdkmanagerin your favorite editor.
  2. Locate the line which sets the DEFAULT_JVM_OPTSvariable. In my copy, it is at line 31:

    DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME"'
    
  3. Append the following options to the variable: -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee. Please pay attention to the quotes.In my copy, the line becomes:

    DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
    
  4. Save the file and quit the editor.
  5. Run the command again.
  1. sdkmanager在您喜欢的编辑器中打开。
  2. 找到设置DEFAULT_JVM_OPTS变量的行。在我的副本中,它位于第 31 行:

    DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME"'
    
  3. 将以下选项附加到变量:-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee. 请注意报价。在我的副本中,该行变为:

    DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee'
    
  4. 保存文件并退出编辑器。
  5. 再次运行命令。

Here is the result:

结果如下:

$ sdkmanager --list
Installed packages:
  Path    | Version | Description              | Location
  ------- | ------- | -------                  | ------- 
  tools   | 26.0.1  | Android SDK Tools 26.0.1 | tools/  

Available Packages:
  Path                              | Version      | Description                      
  -------                           | -------      | -------                          
  add-ons;addon-g..._apis-google-15 | 3            | Google APIs                      
  add-ons;addon-g..._apis-google-16 | 4            | Google APIs                      
  add-ons;addon-g..._apis-google-17 | 4            | Google APIs                      
  add-ons;addon-g..._apis-google-18 | 4            | Google APIs                      
  add-ons;addon-g..._apis-google-19 | 20           | Google APIs                      
  add-ons;addon-g..._apis-google-21 | 1            | Google APIs                      
  add-ons;addon-g..._apis-google-22 | 1            | Google APIs                      
  add-ons;addon-g..._apis-google-23 | 1            | Google APIs                      
  add-ons;addon-g..._apis-google-24 | 1            | Google APIs
...

Hola! It works!

你好!有用!

-- Edit: 2017-11-07 --

-- 编辑:2017-11-07 --

Please note that you may need to apply the fix above again after running sdkmanager --update, since the sdkmanagershell script may be overridden if the toolspackage is updated.

请注意,您可能需要在运行后再次应用上述修复程序sdkmanager --update,因为sdkmanager如果tools更新包,shell 脚本可能会被覆盖。

Related Answers

相关答案

回答by victorjpe

I had a tough time figuring out this solution just adding the working sdkmanager.bat

我很难找到这个解决方案,只是添加了有效的 sdkmanager.bat

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  sdkmanager startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%..

@rem Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Dcom.android.sdklib.toolsdir=%~dp0\.."


@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\lib\sdklib-25.3.1.jar;%APP_HOME%\lib\layoutlib-api-25.3.1.jar;%APP_HOME%\lib\dvlib-25.3.1.jar;%APP_HOME%\lib\repository-25.3.1.jar;%APP_HOME%\lib\gson-2.2.4.jar;%APP_HOME%\lib\commons-compress-1.8.1.jar;%APP_HOME%\lib\httpclient-4.1.1.jar;%APP_HOME%\lib\httpmime-4.1.jar;%APP_HOME%\lib\common-25.3.1.jar;%APP_HOME%\lib\kxml2-2.3.0.jar;%APP_HOME%\lib\annotations-25.3.1.jar;%APP_HOME%\lib\annotations-12.0.jar;%APP_HOME%\lib\jimfs-1.1.jar;%APP_HOME%\lib\httpcore-4.1.jar;%APP_HOME%\lib\commons-logging-1.1.1.jar;%APP_HOME%\lib\commons-codec-1.4.jar;%APP_HOME%\lib\guava-18.0.jar

@rem Execute sdkmanager
"%JAVA_EXE%" %DEFAULT_JVM_OPTS%  -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee  %JAVA_OPTS% %SDKMANAGER_OPTS%  -classpath "%CLASSPATH%" com.android.sdklib.tool.SdkManagerCli %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable SDKMANAGER_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%SDKMANAGER_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

回答by bempa

You can set sdkmanager options with SDKMANAGER_OPTS.

您可以使用 SDKMANAGER_OPTS 设置 sdkmanager 选项。

Example:

例子:

export SDKMANAGER_OPTS="--add-modules java.se.ee"
sdkmanager --list

回答by German

The Android Tools are still incompatible with JDK 9 or 10. You need to install JDK 8 or, if you need multiple Java versions make sure that the system-wide Java home points to a JDK 8.

Android 工具仍然与 JDK 9 或 10 不兼容。您需要安装 JDK 8,或者,如果您需要多个 Java 版本,请确保系统范围的 Java 主页指向 JDK 8。

More details here: How to configure Unity 2017.4 to target Android and avoid build failures on OSX?

更多细节在这里:如何配置 Unity 2017.4 以针对 Android 并避免在 OSX 上构建失败?

回答by Sagar Khatri

For Windows, if nothing works then try this:

对于 Windows,如果没有任何效果,请尝试以下操作:

  • Open sdkmanager.batwith Notepad.

  • Locate the following line:

    %JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SDKMANAGER_OPTS%

  • Add --add-modules java.xml.bind

  • sdkmanager.bat用记事本打开。

  • 找到以下行:

    %JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SDKMANAGER_OPTS%

  • 添加 --add-modules java.xml.bind

The modified line should look like this:

修改后的行应如下所示:

%JAVA_EXE%" %DEFAULT_JVM_OPTS% --add-modules java.xml.bind %JAVA_OPTS% %SDKMANAGER_OPTS%

%JAVA_EXE%" %DEFAULT_JVM_OPTS% --add-modules java.xml.bind %JAVA_OPTS% %SDKMANAGER_OPTS%

回答by valdeci

As we read on the previous comments this error is occurring because the current SDK version is incompatible with newest Java versions: 9 and 10.

正如我们在之前的评论中所读到的,发生此错误是因为当前的 SDK 版本与最新的 Java 版本不兼容:9 和 10。

So, to solve it, you can downgrade your java version to the Java 8 or with as a workaround you can exports the following option on your terminal:

因此,要解决此问题,您可以将 Java 版本降级到 Java 8,或者作为一种解决方法,您可以在终端上导出以下选项:

Linux:

Linux:

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

Windows:

窗户

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

This will solve this error for the sdkmanager

这将解决此错误 sdkmanager

And to make it saved permanently you can export the JAVA_OPTS in 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 thodwris

When having java 11 in the system, the solutions provided are not valid.

当系统中有 java 11 时,提供的解决方案无效。

This -XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee or--add-modules java.xml.bind do not work with Java 11 on Mac OS.

-XX:+IgnoreUnrecognizedVMOptions --add-modules java.se.ee or--add-modules java.xml.bind 不适用于 Mac OS 上的 Java 11。

For that reason you have to downgrade java version to version 8 from here: http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

因此,您必须从这里将 Java 版本降级到版本 8:http: //www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html

List Java versions installed

列出安装的 Java 版本

/usr/libexec/java_home -V

Java 11

爪哇 11

export JAVA_HOME=$(/usr/libexec/java_home -v 11)

Java 1.8

爪哇 1.8

export JAVA_HOME=$(/usr/libexec/java_home -v 1.8)

Then go to

然后去

cd ~/Library/Android/sdk/tools/bin

and

./sdkmanager --licenses

回答by mmccabe

https://adoptopenjdk.netcurrently supports all distributions of JDK from version 8 onwards. For example https://adoptopenjdk.net/releases.html#x64_win

https://adoptopenjdk.net目前支持从版本 8 开始的所有 JDK 发行版。例如https://adoptopenjdk.net/releases.html#x64_win

Here's an example of how I was able to use JDK version 8 with sdkmanager and much more: https://travis-ci.com/mmcc007/screenshots/builds/109365628

这是我如何将 JDK 版本 8 与 sdkmanager 等一起使用的示例:https://travis-ci.com/mmcc007/screenshots/builds/109365628

For JDK 9 (and I think 10, and possibly 11, but not 12 and beyond), the following should work to get sdkmanager working:

对于 JDK 9(我认为是 10,可能是 11,但不是 12 及更高版本),以下应该可以使 sdkmanager 正常工作:

export SDKMANAGER_OPTS="--add-modules java.se.ee"
sdkmanager --list

回答by Jemsheer K D

I was able to solve the issue by using an edited sdkmanager.bat file by forcing to use the Java embedded inside the Android Studio Itself, which i presume uses the OpenJDK 8. Here is the edited sdkmanager I Used :

我能够通过使用已编辑的 sdkmanager.bat 文件来解决该问题,方法是强制使用嵌入在 Android Studio 本身中的 Java,我认为它使用的是 OpenJDK 8。这是我使用的已编辑的 sdkmanager:

@if "%DEBUG%" == "" @echo off
@rem ##########################################################################
@rem
@rem  sdkmanager startup script for Windows
@rem
@rem ##########################################################################

@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%..

@rem Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS="-Dcom.android.sdklib.toolsdir=%~dp0\.."

@rem find Java from Android Studio
@rem Find java.exe
if defined ANDROID_STUDIO_JAVA_HOME goto findJavaFromAndroidStudioJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

goto findJavaNormally

:findJavaFromAndroidStudioJavaHome
set JAVA_HOME=%ANDROID_STUDIO_JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

goto findJavaNormally




@rem java from java home
@rem Find java.exe
:findJavaNormally
if defined JAVA_HOME goto findJavaFromJavaHome

set JAVA_EXE=java.exe
%JAVA_EXE% -version >NUL 2>&1
if "%ERRORLEVEL%" == "0" goto init

goto javaError

:findJavaFromJavaHome
set JAVA_HOME=%JAVA_HOME:"=%
set JAVA_EXE=%JAVA_HOME%/bin/java.exe

if exist "%JAVA_EXE%" goto init

goto javaDirectoryError



:javaError
echo.
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail

:javaDirectoryError
echo.
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
echo.
echo Please set the JAVA_HOME variable in your environment to match the
echo location of your Java installation.

goto fail


:init
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args

:win9xME_args
@rem Slurp the command line arguments.
set CMD_LINE_ARGS=
set _SKIP=2

:win9xME_args_slurp
if "x%~1" == "x" goto execute

set CMD_LINE_ARGS=%*

:execute
@rem Setup the command line

set CLASSPATH=%APP_HOME%\lib\dvlib-26.0.0-dev.jar;%APP_HOME%\lib\jimfs-1.1.jar;%APP_HOME%\lib\jsr305-1.3.9.jar;%APP_HOME%\lib\repository-26.0.0-dev.jar;%APP_HOME%\lib\j2objc-annotations-1.1.jar;%APP_HOME%\lib\layoutlib-api-26.0.0-dev.jar;%APP_HOME%\lib\gson-2.3.jar;%APP_HOME%\lib\httpcore-4.2.5.jar;%APP_HOME%\lib\commons-logging-1.1.1.jar;%APP_HOME%\lib\commons-compress-1.12.jar;%APP_HOME%\lib\annotations-26.0.0-dev.jar;%APP_HOME%\lib\error_prone_annotations-2.0.18.jar;%APP_HOME%\lib\animal-sniffer-annotations-1.14.jar;%APP_HOME%\lib\httpclient-4.2.6.jar;%APP_HOME%\lib\commons-codec-1.6.jar;%APP_HOME%\lib\common-26.0.0-dev.jar;%APP_HOME%\lib\kxml2-2.3.0.jar;%APP_HOME%\lib\httpmime-4.1.jar;%APP_HOME%\lib\annotations-12.0.jar;%APP_HOME%\lib\sdklib-26.0.0-dev.jar;%APP_HOME%\lib\guava-22.0.jar

@rem Execute sdkmanager
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %SDKMANAGER_OPTS%  -classpath "%CLASSPATH%" com.android.sdklib.tool.sdkmanager.SdkManagerCli %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
if "%ERRORLEVEL%"=="0" goto mainEnd

:fail
rem Set variable SDKMANAGER_EXIT_CONSOLE if you need the _script_ return code instead of
rem the _cmd.exe /c_ return code!
if  not "" == "%SDKMANAGER_EXIT_CONSOLE%" exit 1
exit /b 1

:mainEnd
if "%OS%"=="Windows_NT" endlocal

:omega

Here i used an environmental variable ANDROID_STUDIO_JAVA_HOME which actually points to the JRE embedded in the android studio eg: ../android_studio/jre

在这里,我使用了一个环境变量 ANDROID_STUDIO_JAVA_HOME,它实际上指向嵌入在 android studio 中的 JRE,例如:../android_studio/jre

This also has a fallback to JAVA_HOME if ANDROID_STUDIO_JAVA_HOME is not set.

如果 ANDROID_STUDIO_JAVA_HOME 没有设置,这也有一个回退到 JAVA_HOME 。

回答by NatoBoram

The accepted answer is outdated as of February 2019. Here's an answerthat will work until sdkmanagermigrates to a newer version of Java. But by then, you won't have this problem anymore.

已接受的答案截至 2019 年 2 月已过时。这是一个sdkmanager迁移到更新版本的 Java之前将有效的答案。但到那时,你就不会再有这个问题了。

OpenJDK 10 was superseeded by OpenJDK 11, which doesn't implement java.se.eeat all. This means that the hack of adding --add-modules java.se.eedoesn't do anything anymore. It also means that OpenJDK 10 will be automatically removed from your system and replaced with OpenJDK 11 the next time you update, if your updates are configured properly.

Modify sdkmanagerto use Java 8 by setting JAVA_HOMEinsidesdkmanagerto a Java 8 installation. It's, by default, at ~/Android/Sdk/tools/bin/sdkmanager.

# Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options $
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions'
@rem Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options to this script.
set JAVA_HOME="C:\ProgramData\scoop\apps\android-studio\current\jre"
set DEFAULT_JVM_OPTS="-Dcom.android.sdklib.toolsdir=%~dp0\.."

This way, you can keep using a sane and maintained version of Java on your system while simultaneously using sdkmanager.

# Java
export JAVA_HOME=/usr/lib/jvm/default-java

And now I've got some pipelinesto repair.

OpenJDK的10是由OpenJDK的11 superseeded,它没有实现java.se.ee在所有。这意味着添加的黑客--add-modules java.se.ee不再做任何事情。这也意味着 OpenJDK 10 将自动从您的系统中删除,并在您下次更新时替换为 OpenJDK 11,如果您的更新配置正确。

sdkmanager通过将JAVA_HOMEinside设置sdkmanager为 Java 8 安装来修改以使用 Java 8 。默认情况下,它位于~/Android/Sdk/tools/bin/sdkmanager

# Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options $
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-amd64
DEFAULT_JVM_OPTS='"-Dcom.android.sdklib.toolsdir=$APP_HOME" -XX:+IgnoreUnrecognizedVMOptions'
@rem Add default JVM options here. You can also use JAVA_OPTS and SDKMANAGER_OPTS to pass JVM options to this script.
set JAVA_HOME="C:\ProgramData\scoop\apps\android-studio\current\jre"
set DEFAULT_JVM_OPTS="-Dcom.android.sdklib.toolsdir=%~dp0\.."

这样,您可以在系统上继续使用一个健全且维护良好的 Java 版本,同时使用sdkmanager.

# Java
export JAVA_HOME=/usr/lib/jvm/default-java

现在我有一些管道要修理。