3

Since StackExchange advised me to not ask for help or clarification on a previously existing post, I am making a new thread.

On Windows 10x64 I downloaded the Eclipse SWT (in second-to-last section of Downloads page), set my ANDROID_SWT environment variable to <download location>\swt.jar, and have attempted to run uiautomatorviewer from cmd again, only to receive the same error message as when I tried to use the SWT bundled with eclipse:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Listener
    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)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Listener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

Has anyone else encountered this error, and if so, what was the root cause?

Thanks in advance for your time and help!

Wanda B.
  • 161
  • 2
  • 11
  • Since you got an exception and not a compile error, what is happening is that Eclipse is aware of the library for building, but your deployment environment does not know where to find the SWT jar. Please [edit] your question and explain how you are deploying and running your project. – Jim Garrison Feb 10 '18 at 07:04
  • Hi, @JimGarrison! I am attempting to run `uiautomatorviewer` from cmd, and therefore I believe I only need to set the `ANDROID_SWT` path variable. However, despite my having set this variable, as well as having added Java to my `PATH` variable, I am receiving the above exception. – Wanda B. Feb 10 '18 at 07:07
  • You need to add the `swt.jar` file to the classpath. Try that. – Jim Garrison Feb 10 '18 at 07:10
  • Hmmm, I made a new variable `CLASSPATH` since it did not already exist in my Environment Variables, and added the absolute path of `swt.jar` downloaded from Eclipse, and yet I receive the same exception... I'm going to reboot my computer in case the changes to envionrment variables only takes effect when you logout and create a new session! – Wanda B. Feb 10 '18 at 07:16
  • Here are some of the environment variables I've set so far: 1) ANDROID_HOME - location of .\Android\sdk (within the Android Studio directory) 2) ANDROID_SWT - location of swt.jar 3) CLASSPATH - location of swt.jar 4) PATH - appended bin directory for JDK 8 (I read that JDK 9 is incompatible with uiautomatorviewer), as well as %ANDROID_HOME%\platform-tools and the standalone adb tools directory... I am still experiencing the same exception. Thank you again for your continued help with this issue! – Wanda B. Feb 10 '18 at 07:20
  • @JimGarrison I have solved the issue, as per my follow-up comment :) Thanks for commenting! – Wanda B. Feb 10 '18 at 08:37

4 Answers4

2

In my case adding ANDROID_SWT to environment variables or editing PATH didn't solve the problem. Therefore I found a different solution via help from this question.

Running uiautomatorviewer.bat and output :

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/eclipse/swt/widgets/Control
    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:650)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:632)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.widgets.Listener
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)

Solution :

Go to : https://www.findjar.com/ and search for missing class. In this case search for class :

org.eclipse.swt.widgets.Control

For your case it is:

org.eclipse.swt.widgets.Listener

Names of the classes in the error can change, but all the classes needed comes from .jar file that will be downloaded.

In the results click :

[CLASS] org.eclipse.swt.widgets.Control

or for your case :

[CLASS] org.eclipse.swt.widgets.Listener

Download latest and suitable file for your OS. This was the latest and suitable for Windowsx86_64 currently.

Put downloaded .jar file into :

C:\Users\{userId}\AppData\Local\Android\Sdk\tools\lib

or equivalent path for your OS.

Now uiautomatorviewer.bat should run without any problems.

enes
  • 91
  • 8
1

In my case the exception was

Exception in thread "main" java.lang.NoClassDefFoundError: 
  org/eclipse/swt/custom/TableTreeItem
at org.eclipse.jface.util.OpenStrategy.initializeHandler(OpenStrategy.java:270)
at org.eclipse.jface.util.OpenStrategy.<init>(OpenStrategy.java:108)
at org.eclipse.jface.viewers.StructuredViewer.hookControl(StructuredViewer.java:1213)
at org.eclipse.jface.viewers.ColumnViewer.hookControl(ColumnViewer.java:68)
at org.eclipse.jface.viewers.AbstractTreeViewer.hookControl(AbstractTreeViewer.java:1452)
at org.eclipse.jface.viewers.TreeViewer.hookControl(TreeViewer.java:274)
at org.eclipse.jface.viewers.TreeViewer.<init>(TreeViewer.java:137)
at org.eclipse.jface.viewers.TreeViewer.<init>(TreeViewer.java:124)
at com.android.uiautomator.UiAutomatorView.<init>(UiAutomatorView.java:361)
at com.android.uiautomator.UiAutomatorViewer.createContents(UiAutomatorViewer.java:66)
at org.eclipse.jface.window.Window.create(Window.java:431)
at org.eclipse.jface.window.Window.open(Window.java:790)
at com.android.uiautomator.UiAutomatorViewer.main(UiAutomatorViewer.java:78)
Caused by: java.lang.ClassNotFoundException: org.eclipse.swt.custom.TableTreeItem
at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
... 13 more

Steps followed for the solution

  1. Go to https://jar-download.com/
  2. Search for the missing jar file in my case it was org.eclipse.swt.custom.tabletreeitem
  3. Download the jar file compatible with your system Mac/Win/Linux (In my case it was mac enter image description here
  4. Extract the jar file and copy it
  5. Go to the lib folder inside SDK (Android/Sdk/Tools/Lib
  6. Paste the jar file into the lib folder
  7. Restart your terminal or cmd
  8. Run the command again for cd $ANDROID_HOME/tools/bin
  9. Run the command ./uiautomatorviewer

Alternatively

Try to download the swt jar file with a different version and copy it to the respective folders. Follow this thread :

https://github.com/android/android-test/issues/911#issuecomment-849389068

0

Make sure JDK location selected in Android studio is same as JDK set in environment variable.

enter image description here

Community
  • 1
  • 1
-1

Precisely follow these instructions. I made the mistake of trying to download the Eclipse SWT in order to troubleshoot other issues I was having; setting this different SWT version in the CLASSPATH and ANDROID_SWT environment variables caused this exception.

Wanda B.
  • 161
  • 2
  • 11