java 无法找到或加载主类 org.junit.runner.JUnitCore

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28261167/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 13:17:29  来源:igfitidea点击:

Could not find or load main class org.junit.runner.JUnitCore

javajunitwindows-7

提问by J.Olufsen

I packed test classes into JAR. I have junit-4.10.jarand aJar.jarin the same folder. When I try to execute:

我将测试类打包到 JAR 中。我有junit-4.10.jaraJar.jar在同一个文件夹中。当我尝试执行时:

java -cp .:junit-4.10.jar org.junit.runner.JUnitCore TestOne

Error: Could not find or load main class org.junit.runner.JUnitCore

How to make it work?

如何使它工作?

When I type: java aJar.jar:junit-4.10.jar org.junit.runner.JUnitCore TestOne

当我输入: java aJar.jar:junit-4.10.jar org.junit.runner.JUnitCore TestOne

I am getting

我正进入(状态

Error: Could not find or load main class aJar.jar:junit-4.10.jar

采纳答案by JB Nizet

You seem to be running under Windows, not LINUX/UNIX. The path separator on Windows is ;, not :. Additionally, you haven't put you jar file in the classpath. So what you want is:

您似乎是在 Windows 下运行,而不是在 LINUX/UNIX 下运行。Windows 上的路径分隔符是;, 不是:。此外,您还没有将 jar 文件放在类路径中。所以你想要的是:

java -cp aJar.jar;junit-4.10.jar org.junit.runner.JUnitCore TestOne

This of course assumes that both jars are in the current directory. You should also always avoid putting classes in the default package.

这当然假设两个 jars 都在当前目录中。您还应该始终避免将类放在默认包中。

回答by huguinho27

I had recently the same problem (JUnit 4.12) and I managed to solve it (Windows) by the following command:

我最近遇到了同样的问题(JUnit 4.12),我设法通过以下命令解决了它(Windows):

java -cp "<libs>;<relative path to .class project folder>" org.junit.runner.JUnitCore <package.ClassName>

Import to notice the "" in the classpath declaration and separated by ";"

导入以注意类路径声明中的“”并以“;”分隔