Just in case you have multiple versions of Java installed on your OS, and the active Java version is higher than the expected one, you may end up with UnsupportedClassVersionError
exception.
In my MacOS case, the active Java version is Java 17.
~ java -version
openjdk version "17.0.5" 2022-10-18 LTS
OpenJDK Runtime Environment Zulu17.38+21-CA (build 17.0.5+8-LTS)
OpenJDK 64-Bit Server VM Zulu17.38+21-CA (build 17.0.5+8-LTS, mixed mode, sharing)
and the expected Java version is Java 11 as of now.
so my flutter doctor --android-licenses
command ended up with following error:
flutter doctor --android-licenses
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.UnsupportedClassVersionError:
com/android/prefs/AndroidLocationsProvider has been compiled by a
more recent version of the Java Runtime (class file version 55.0),
this version of the Java Runtime only recognizes class file
versions up to 52.0
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:757)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
at java.lang.Class.getMethod0(Class.java:3018)
at java.lang.Class.getMethod(Class.java:1784)
at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Obviously, Java 11 (class file version 52.0) rather than Java 17(class file version 55.0) is required.
Solution is exporting JAVA_HOME
(preferred in a command line windown) and run flutter doctor --android-licenses
again, then it should work.
export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/
flutter doctor --android-licenses
By the way, installed Javas can be detected by Android Studio.
(Pasted my previous comment) Again, I don't think it's a good idea to remove and then re-install a new specific Java version just for the purpose of having flutter doctor --android-licenses work. Actually, it's more appropriate to switch to another Java version if you have multiple versions of Java installed, by exporting the desired one. for example:export JAVA_HOME=/Library/Java/JavaVirtualMachines/zulu-11.jdk/Contents/Home/. Internally, flutter doctor will check JAVA_HOME for the target Java version. The benefit is obvious, the one-time export command in a command line window won't affect anything else at all.