Exception in thread “main” java.lang.NoClassDefFoundError: junit/textui/ResultPrinter

不羁岁月 提交于 2019-12-17 10:56:09

问题


I'm trying to compile my Android project in Android Studio 0.3.0. Today I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:188)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:113)
Caused by: java.lang.ClassNotFoundException: junit.textui.ResultPrinter
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
... 3 more

Process finished with exit code 1

Doing some web searches leads me to believe this issue is somehow related to JUnit. However, I'm not using JUnit in my project. Maybe I've inadvertently turned on some option? In that case, how do I disable unit testing in my project? Any ideas?


回答1:


Finally found it. It's in the Run/Debug Configurations dialog. Disabled JUnit and it compiles again.




回答2:


Usually this problem is caused because of wrong test type: Junit instead of Android Test




回答3:


For standard JUnit tests, you are missing the JUnit library, try adding this in build.gradle:

dependencies {
    ...
    testCompile 'junit:junit:4.12'
    ...
}

And make sure you are putting the tests in the "test" directory parallell to the "main" package (instrumentation tests goes into "androidTest", but that is different setup).




回答4:


Force the project to "sync" via menu: Tools > Android > Sync Project with Gradle Files or force the project to "sync" by making a fake change in build.gradle file (add a space and then delete it and then click "sync now").




回答5:


In my Run/Debug Configurations I had MyTest under AndroidTests and under JUnitTests. The MyTest under AndroidTests was configured correctly, but I still got 'NoClassDefFoundError: junit/textui/ResultPrinter' error. Clicked on MyTest under JUnit and pressed '-' in upper left. This allowed MyTest to run through the device got rid of error.




回答6:


I've twice had this issue after changing some build.gradle plugins and dependencies around.

Simply turning Android Studio off and on again fixed it for me (versions 2.1.2 and 2.2).




回答7:


All the answers are good-

Make sure you have sourceSets have the test directory registered.

android{
    ...

    defaultConfig {
         ...
         testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }


    sourceSets {
        androidTest {
            java.srcDirs = ['src/androidTest/java']
        }
    }
}

dependencies{
    ... 
    androidTestCompile 'junit:junit:4.12'
    androidTestCompile 'com.android.support.test:runner:0.5'
}



回答8:


Just putting the solution which worked for me, this should be tried if you are setting up the environment for first time:

For windows: 1) in the environment variables add a new "system variables" ANDROID_SDK_HOME=D:\Program Files\android-sdk-windows (select your home directory of android sdk )

2) modify system variables Path, add "%Android_SDK_HOME%\tools;"



来源:https://stackoverflow.com/questions/19516289/exception-in-thread-main-java-lang-noclassdeffounderror-junit-textui-resultpr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!