问题描述
我在Mac上配置了Junit-4.11,使用javac
进行编译没有错误,但是当我使用java
运行时,得到了Could not find class: HelloWorldTest
I configured Junit-4.11 on my Mac, compile with javac
has no error, but when I run with java
, I got Could not find class: HelloWorldTest
这是我的HelloWorld.java
和HelloWorldTest.java
import java.util.*;
public class HelloWorld {
public String output() {
return "Hello world!";
}
}
import static org.junit.Assert.*;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.Test;
import java.util.*;
import org.junit.*;
public class HelloWorldTest {
public HelloWorld helloworld = new HelloWorld();
@BeforeClass
public static void oneTimeSetUp() {
System.out.println("@BeforeClass - oneTimeSetUp");
}
@AfterClass
public static void oneTimeTearDown() {
System.out.println("@AfterClass - oneTimeTearDown");
}
@Before
public void setUp() {
System.out.println("@Before - setUp");
}
@After
public void tearDown() {
System.out.println("@After - tearDown");
}
@Test
public void testOutput() {
assertEquals(helloworld.output(), "Hello world!");
System.out.println("@Test - testOutput");
}
}
然后我用
javac -classpath ./ HelloWorldTest.java
和java -classpath ./ org.junit.runner.JUnitCore HelloWorldTest
我得到的是
JUnit version 4.11
Could not find class: HelloWorldTest
Time: 0.002
OK (0 tests)
我将junit-4.11.jar
与HelloWorld.java
和HelloWorldTest.java
放在当前目录中,我也将其放在/Library/Java/Extensions
I put junit-4.11.jar
in the current directory with HelloWorld.java
and HelloWorldTest.java
, I also put it in /Library/Java/Extensions
我想解决的是设置JAVA_HOME
和CLASSPATH
,但是没有用.
What I tried to solve is to set JAVA_HOME
and CLASSPATH
, but it did not work.
有人能指出出什么问题了吗?我真的很困惑.
Could someone point out what was going wrong? I was really confused.
谢谢.
我已经通过以下步骤解决了我的问题.我的Mac是Mac OSX 10.8,我使用了Apple提供的JVM-1.6.您可以通过点击此处进行下载.
Well I have solved my problem with the following steps. My Mac is Mac OSX 10.8 and I used JVM-1.6 provided by Apple. You can download it by clicking here.
- 删除我的
.zshrc
文件中的CLASSPATH
(如果使用的是Bash
,我认为它是.bashrc
) - 删除
/Library/Java/Extensions
中的JUnit-4.11.jar
(或您使用的任何版本)以及放置在其中的任何系统目录. - 尝试再次编译并运行它.
- Delete the
CLASSPATH
in my.zshrc
file(If you are usingBash
I think it is.bashrc
) - Delete the
JUnit-4.11.jar
(or any version you use) in the/Library/Java/Extensions
and any System Directory you put it in. - Try to compile again and run it.
然后将JAVA_HOME
设置为/System/Library/Frameworks/JavaVM.framework/Versions/1.6/Home
谢谢.
推荐答案
更新:原始发布者的解决方案埋在下面的评论中.这与在/Library/Java/Extensions
中没有junit jar文件并且没有CLASSPATH
有关:
Update: The original poster's solution is buried in a comment below. It has to do with not having the junit jar files in /Library/Java/Extensions
, and not having a CLASSPATH
:
在临时目录中(来自您问题的Java文件):
In a temp dir (java files from your question):
HelloWorld.java
HelloWorldTest.java
junit-4.11.jar
hamcrest-core-1.3.jar
然后:
-
javac -cp junit-4.11.jar *.java
-
java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore HelloWorldTest
javac -cp junit-4.11.jar *.java
java -cp junit-4.11.jar:hamcrest-core-1.3.jar:. org.junit.runner.JUnitCore HelloWorldTest
输出:
HelloWorldTest
JUnit version 4.11
@BeforeClass - oneTimeSetUp
.@Before - setUp
@Test - testOutput
@After - tearDown
@AfterClass - oneTimeTearDown
Time: 0,004
OK (1 test)
我建议您从头开始重试
这篇关于在Junit-4.11中找不到课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!