31

I am trying to run uiautomatorviewer in terminal. I am getting this error:

-Djava.ext.dirs=/Users/<Username>/Library/Android/sdk/tools/lib/x86_64:/Users/<Username>/Library/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 think this might be related to the java version I am using. Here is the output of java -version:

java version "10" 2018-03-20
Java(TM) SE Runtime Environment 18.3 (build 10+46)
Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10+46, mixed mode)

I have already seen this question on SO, but it recommends downgrading to java 8.

Am I missing anything here? I would appreciate any help.

Praveen Singh
  • 2,499
  • 3
  • 19
  • 29

11 Answers11

31

uiautomatorviewer works fine with Java version 8. You need not downgrade java version to 1.8. Instead add this method to .bash_profile

uiautomator() {
        export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_181.jdk/Contents/Home
        export ANDROID_HOME=/Users/<username>/Library/Android/sdk
        export PATH=$PATH:$ANDROID_HOME/platform-tools:$ANDROID_HOME/build-tools:$ANDROID_HOME/tools
        /Users/<username>/Library/Android/sdk/tools/bin/uiautomatorviewer
}

This would set the JAVA_HOME path to 1.8 and includes android platform and build tools to path variable.

Either,

  1. Restart your terminal to get the changes reflected or
  2. Compile your .bash_profile by . .bash_profile in the current terminal to get it reflected.

Start typing uiautomator in your terminal. It should work !

Tom Taylor
  • 3,344
  • 2
  • 38
  • 63
14

Djava.ext.dirs=/Users//Library/Android/sdk/tools/lib/x86_64:/Users//Library/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.

Following worked for me (Ubuntu 16.04, Java 11):

$ export YOUR_SDK_PATH="/home/user/Android/Sdk/"

$ java -Xmx1600M -Dcom.android.uiautomator.bindir="$YOUR_SDK_PATH/tools" -cp "$YOUR_SDK_PATH/tools/lib/x86_64/swt.jar":"$YOUR_SDK_PATH/tools/lib/*" com.android.uiautomator.UiAutomatorViewer
Kosta B.
  • 271
  • 2
  • 12
Leo77
  • 141
  • 2
  • 3
  • 1
    ``` Error: Could not find or load main class com.android.uiautomator.UiAutomatorViewer Caused by: java.lang.ClassNotFoundException: com.android.uiautomator.UiAutomatorViewer ``` I get this error – Farees Hussain Oct 08 '21 at 07:10
8

uiautomatorviewer is not compatible with java10 as far as I can tell. Switching to java8 fixed it for me.

jgoldman33
  • 91
  • 1
  • 4
5

Right click on uiautomator.bat file and click on edit.(should open up note pad with code in it)

Scroll and find "rem Check we have a valid Java.exe in the path."

Make sure the next 2 lines of code I have below should be the same ie make the changes so its the same.

set java_exe=C:\Program Files\Android\Android Studio\jre\bin\java.exe

rem call ..\lib\find_java.bat Save file and it will work

sandra a.j
  • 51
  • 1
  • 1
5

use Layout Inspector instead. its better.

enter image description here

Tim Boland
  • 1,872
  • 4
  • 23
  • 37
5

I'm on Ubuntu 20.04;Download java8 Compressed Archive from here ,uncompress it to somewhere, then open uiautomatorviewer with some text editor like gedit,and replace

javaCmd="java"

to

javaCmd="<you uncompressed java8 dir location>/bin/java"

Then it may work.

JiajiaGu
  • 1,279
  • 14
  • 10
2

Moving back Java 8 has fixed the problem for me. After you have installed Java 8, make sure your environment variables are not pointing to your previously installed Java.

rajkabbur
  • 187
  • 3
1

I also faced the same issue and i did the following in my uiautomatorviewer file. Instead of this line:

exec "${javaCmd}" $javaOpts -Djava.ext.dirs="$frameworkdir" -Dcom.android.uiautomator.bindir="$progdir" -jar "$jarpath" "$@"

I replaced the same using Classpath and it worked:

exec java -Xmx1600M -XstartOnFirstThread -Dcom.android.uiautomator.bindir=/your-sdk-path/tools -cp /your-sdk-path/tools/lib/x86_64/swt.jar:/your-sdk-path/lib/uiautomatorviewer.jar:/your-sdk-path/tools/lib/*.jar:. com.android.uiautomator.UiAutomatorViewer

Replace your-sdk-path with the location of sdk on your system. You can try this command directly on your terminal also and it will launch 'UiAutomatorViewer' without doing any changes around java.

Juan Antonio
  • 2,451
  • 3
  • 24
  • 34
0

I faced the same issue, and I tried all the solutions the people suggested, but it did not work. The only solution I found is below:

  1. Install/downgrade your Java version to 8. In my case, I had Java version 17 the latest.
  2. Update JAVA_HOME value in the environment variable with (C:\Program Files\Java\jdk1.8.0_311).
  3. RUN command Prompt(cmd) as administrator
  4. Write (uiautomatorviewer)
  5. Hit the ENTER. Check below image to show you before enter image description here& after applied the solution.
Anas Satti
  • 11
  • 4
0

Works for me on Linux, but not my friend's Mac.

In ./Android/Sdk/tools/bin/uiautomatorviewer

I replaced

exec "${javaCmd}" $javaOpts -Djava.ext.dirs="$frameworkdir" \
  -Dcom.android.uiautomator.bindir="$progdir" -jar "$jarpath" "$@"

with

exec "${javaCmd}" $javaOpts \
  -Dcom.android.uiautomator.bindir="$progdir" \
  -cp $swtpath/swt.jar:$frameworkdir/* com.android.uiautomator.UiAutomatorViewer "$@"

using the /* CLASSPATH wildcard.

Tim Baverstock
  • 415
  • 4
  • 11
0

this was a real adventure....

  1. specifically get JDK version 8 (not JRE or another version) https://www.oracle.com/java/technologies/javase/javase8u211-later-archive-downloads.html

  2. add JDK8 to your environemnt path as JAVA_HOME..for example: enter image description here

  3. make sure %ANDROID_HOME%\tools\bin is also added to your Path as well so that you can run uiautomatorviewer from any directory enter image description here

  4. in terminal, run uiautomatorviewer

Eric
  • 16,397
  • 8
  • 68
  • 76