Failed to run sdkmanager --list (Android SDK) with Java 9

匿名 (未验证) 提交于 2019-12-03 02:13:02

问题:

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.

After configuring the PATH variable, I tried running sdkmanager, which replaced the android command for managing SDK components. However, it failed as shown here:

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

Related Questions

  • Failed to install android-sdk
    • This post asked a similar question. However, the post is closed and the only answer suggests going back to Java 8.

回答1:

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_sdk is where you unzipped the Android SDK.

  1. Open sdkmanager in 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.

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

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

Related Answers



回答2:

You can set sdkmanager options with SDKMANAGER_OPTS.

Example:

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


回答3:

I had a tough time figuring out this solution just adding the working 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 


回答4:

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.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!