线程“main”中的异常 java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19516289/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Exception in thread "main" java.lang.NoClassDefFoundError: junit/textui/ResultPrinter
提问by Janne Oksanen
I'm trying to compile my Android project in Android Studio 0.3.0. Today I get the following error:
我正在尝试在 Android Studio 0.3.0 中编译我的 Android 项目。今天我收到以下错误:
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.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?
做一些网络搜索让我相信这个问题与 JUnit 有某种关系。但是,我没有在我的项目中使用 JUnit。也许我无意中打开了某个选项?在这种情况下,如何在我的项目中禁用单元测试?有任何想法吗?
采纳答案by Janne Oksanen
Finally found it. It's in the Run/Debug Configurations dialog. Disabled JUnit and it compiles again.
终于找到了。它位于“运行/调试配置”对话框中。禁用 JUnit 并再次编译。
回答by techExplorer
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 )
对于 Windows: 1) 在环境变量中添加一个新的“系统变量” ANDROID_SDK_HOME=D:\Program Files\android-sdk-windows (选择您的 android sdk 主目录)
2) modify system variables Path, add "%Android_SDK_HOME%\tools;"
2)修改系统变量Path,添加“%Android_SDK_HOME%\tools;”
回答by Pnemonic
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").
通过菜单强制项目“同步”:Tools > Android > Sync Project with Gradle Files 或通过在 build.gradle 文件中进行虚假更改来强制项目“同步”(添加一个空格然后删除它,然后单击“同步”现在”)。
回答by radistao
Usually this problem is caused because of wrong test type: Junitinstead of Android Test
通常这个问题是由于错误的测试类型引起的:Junit而不是Android Test
回答by flobacca
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.
在我的运行/调试配置中,我在 AndroidTests 和 JUnitTests 下有 MyTest。AndroidTests 下的 MyTest 配置正确,但我仍然收到 'NoClassDefFoundError: junit/textui/ResultPrinter' 错误。单击 JUnit 下的 MyTest,然后按左上角的“-”。这允许 MyTest 运行通过设备摆脱错误。
回答by powder366
For standard JUnit tests, you are missing the JUnit library, try adding this in build.gradle:
对于标准 JUnit 测试,您缺少 JUnit 库,请尝试在 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).
并确保您将测试放在与“main”包平行的“test”目录中(仪器测试进入“androidTest”,但这是不同的设置)。
回答by Tom
I've twice had this issue after changing some build.gradle
plugins and dependencies around.
在更改了一些build.gradle
插件和依赖项后,我已经两次遇到这个问题。
Simply turning Android Studio off and on again fixed it for me (versions 2.1.2 and 2.2).
只需关闭并再次打开 Android Studio 即可为我修复它(版本 2.1.2 和 2.2)。
回答by Manisha
All the answers are good-
所有的答案都很好-
Make sure you have sourceSets have the test directory registered.
确保您的 sourceSets 已注册测试目录。
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'
}