2

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

Ali
  • 23
  • 5
  • 2
    Your question is unclear. If you used `--class-path`, the Java command won't recognize it. If you used `-classpath` like the error message said ... please show us **exactly** what you changed the `uiautomatorviewer` script to. – Stephen C Apr 28 '21 at 12:55
  • Note that the `ext.dirs` mechanism wasn't removed until Java 9, so it it doubtful that you are actually *using* Java 8 to run the program. – Stephen C Apr 28 '21 at 12:57
  • @StephenC Hi and thanks for your time. for your second comment: I used both, but it doesn't work. my main issue is: 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. I changed the file according to this link: https://www.programmersought.com/article/53371586152/ And what can I do for solving this problem? – Ali Apr 28 '21 at 13:08
  • The solution is to either *really* use Java 8, OR use `-classpath` ... *correctly*. I can't help you further unless you provide the information that I requested. Please provide the information by [EDITING](https://stackoverflow.com/posts/67300521/edit) the question. Not as comments. – Stephen C Apr 28 '21 at 13:08
  • You still haven't done what I requested. Please show me the changes that you made to the `uiautomatorviewer` script. (I don't want to see the instructions that you think you followed. I want to see the *actual* changes.) – Stephen C Apr 28 '21 at 13:14
  • @StephenC I added the two photos of uiautomatorviewer file. once is original another one is my changes – Ali Apr 28 '21 at 13:29
  • Sigh ... [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Stephen C Apr 28 '21 at 13:30
  • Add this line before the `exec` line, and show us all of the console output: `set -x ; ${javaCmd} -version` – Stephen C Apr 28 '21 at 13:34
  • +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.Useclasspath – Ali Apr 28 '21 at 13:47

1 Answers1

1

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216