0

As titled,I recently got NoClassDefFoundError exception when running my android test.And the class is a custom class.I know that might be the problem of the classpath,but I don't know where to set the classpath on Ubuntu.

[UPDATE]

This is the class that invokes the error when getIdlingResource() is called:

public final class ToastManager {
    private static final CountingIdlingResource idlingResource = new CountingIdlingResource("toast");
    private static final View.OnAttachStateChangeListener listener = new View.OnAttachStateChangeListener() {
    @Override
    public void onViewAttachedToWindow(final View v) {
        idlingResource.increment();
    }

    @Override
    public void onViewDetachedFromWindow(final View v) {
        idlingResource.decrement();
    }
};

private ToastManager() { }

public static Toast makeText(final Context context, final CharSequence text, final int duration) {
    Toast t = Toast.makeText(context, text, duration);
    t.getView().addOnAttachStateChangeListener(listener);
    return t;
}

// For testing
public static IdlingResource getIdlingResource() {
    return idlingResource;
}
}  

This is the error log:

java.lang.NoClassDefFoundError:   com/idreamsglobal/idreams/utils/ToastUtil
at com.idreamsglobal.idreams.RegisterActivityTest.tearDown(RegisterActivityTest.java:168)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:33)
at android.support.test.internal.statement.UiThreadStatement.evaluate(UiThreadStatement.java:55)
at android.support.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:257)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at android.support.test.internal.runner.TestExecutor.execute(TestExecutor.java:54)
at android.support.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:240)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1741)
Tony Chen
  • 207
  • 2
  • 13

2 Answers2

0

I solved the problem combining Jerome's and Jarod's answer:

  1. Install JDK -- sudo apt-get install openjdk-7-jdk
  2. Environment Variable -- sudo nano /etc/environment adding the following line:
  3. JAVA_HOME=/usr/lib/jvm/java-1.7.0-openjdk-i386
  4. Reboot, and Android Studio starts up. (I had added also a link to studio.sh to the main menu).

Also I just found https://stackoverflow.com/a/17827697/2533809 which seems to have a nice write-up, pretty much the same answer.

(I'm using Debian 7 Wheezy)

Reference link

Community
  • 1
  • 1
Muhammad Waleed
  • 2,517
  • 4
  • 27
  • 75
0

In the 'project' view, right-click on 'app' module and choose 'Open Module Settings'. Then Click on 'Dependencies' tab on the top. The box below would list the classpath. By default, it includes *.jar as well.

If ToastUtil that you are using is in a jar, I guess it should be added by default. However, try add that jar manually under dependencies. If it is a file that you wrote, check if you have properly imported.

Also, simply try moving the file out of the project using the terminal and build the code for errors. And then add it again in the proper package and try a clean build. Sometimes, that would do the trick.

Hope that helps!

Ramraj
  • 2,040
  • 14
  • 17