Search code examples
javaandroidandroid-studiouiautomatorviewer

Getting an error while running uiautomatorviewer in ubuntu 20.04


The following error is displayed when the command is executed:

username@device:~/Android/Sdk/tools/bin$ ./uiautomatorviewer

Djava.ext.dirs=/home/username/Android/Sdk/tools/lib/x86_64:/home/username/Android/Sdk/tools/lib is not supported. Use -classpath instead. Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit.


then I use --class-path in uiautomatorviewer file and face this error: I use this method for editing uiautomatorviewer file: https://www.programmersought.com/article/53371586152/

error: unable to initialize main class com.android.uiautomator.uiautomatorviewer caused by: java.lang.noclassdeffounderror: org/eclipse/swt/widgets/control mychange:enter image description here original:enter image description here And when I got the above error, I turned back to the previous state

And now I have the first issue again.

extera information: $JAVA_HOME => /usr/lib/jvm/java-8-openjdk-amd64

$ANDROID_HOME => /home/username/Android/Sdk

Android studio version: 4.1.3


Solution

  • This is the output that I asked for in the comments.

    +java -version 
    openjdk version "11.0.11" 2021-04-20 
    OpenJDK Runtime Environment (build 11.0.11+9-Ubuntu-0ubuntu2.20.04)
    OpenJDK 64-Bit Server VM (build 11.0.11+9-Ubuntu-0ubuntu2.20.04, mixed mode, sharing) 
    +exec java -Xmx1600M -Djava.ext.dirs=/home/alielyasi/Android/Sdk/tools/lib/x86_64:/home/alielyasi/Android/Sdk/tools/lib -Dcom.android.uiautomator.bindir=/home/alielyasi/Android/Sdk/tools -jar /home/alielyasi/Android/Sdk/tools/lib/uiautomatorviewer-26.0.0-dev.jar
    -Djava.ext.dirs=/home/alielyasi/Android/Sdk/tools/lib/x86_64:/home/alielyasi/Android/Sdk/tools/lib is not supported.
    Use -classpath 
    

    Firstly, we can see that the Java command that is being used is java, not a specific (absolute) pathname.

    That is OK, but it means that $PATH will be used to resolve java to the actaul pathname of a Java launcher. (Not $JAVA_HOME.)

    Second, we can see that java -version is telling us that it is using Java 11.0.11. As I mentioned in my comments, Java 9 and later do not support the "ext.dirs" mechanism.

    Unfortunately, the java command's suggestion to use -classpath is not going to work (as-is).

    So I am going to give you two ways to solve this:

    Solution #1: Just use Java 8.

    You need to make sure that you have Java 8 installed, and that running the java -version prints out that that the version number is 8.0.xxx, where xxx is (ideally) the latest available Java 8 patch release.

    Since you are using using Ubuntu 20.04, there are two ways to make sure that you are using Java 8 rather than Java 11.

    • You could make Java 8 the default Java version globally using the "alternatives" system. Run man update-alternatives for the documentation. (You need to run update-alternatives as root ...)

    • You could make Java 8 the default for the current user or the current shell by changing the PATH environment variable.

    Solution #2: Adjust the script so that Java 11 will work.

    As I said above, simply converting -Djava.ext.dirs=... into a -classpath option doesn't work. That is because the exec line is using the -jar option, and when -jar is used -classpath is ignored. But -jar means this is an "executable JAR", and the entrypoint class name is coming from the JAR file's manifest.

    So we need to unpick this.

    First we need to know what the (full) entrypoint class name is, and also JAR file is setting its own classpath. We can determine by using the jar command to extract the META-INF/MANIFEST.MF file and looking it.

    Next we need to form a classpath consisting of

    • the path that is currently given by $frameworkdir
    • the path for the main JAR file (i.e. $jarpath)
    • the path specified in the manifest file

    The order of the path could be significant.

    The entire classpath needs to be given to the java command via a -classpath or -cp option. (Not --class-path.)

    Finally remove -jar $jarpath and replace it with the full entrypoint class name. (It is a class name, not a pathname. Don't change the dots to slashes, add a suffix or do anything like that. Use the name exactly as the manifest file gives it.)

    This approach should work, but I can't test it. If it fails, please comment below.