我在我的Ubuntu中搜索了junit.jar
$ locate -i junit.jar
/home/homebin/matlabR2010b/java/jarext/junit.jar
/home/tim/program_files/programming/eclipse/plugins/org.apache.ant_1.9.2.v201404171502/lib/ant-junit.jar
/home/tim/program_files/programming/eclipse/plugins/org.junit_4.11.0.v201303080030/junit.jar
然后我想我选择第三个来指定junit.jar的路径,但我仍然有The import org.junit cannot be resolved
错误。
$ javac -cp "/home/tim/program_files/programming/eclipse/plugins" MyTest.java
MyTest.java:3: error: The import org.junit cannot be resolved
import org.junit.runner.JUnitCore;
^^^^^^^^^
MyTest.java:4: error: The import org.junit cannot be resolved
import org.junit.runner.Result;
^^^^^^^^^
MyTest.java:5: error: The import org.junit cannot be resolved
import org.junit.runner.notification.Failure;
^^^^^^^^^
更新
MyTest.java
位于/tmp
下,其内容为
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
public class MyTest {
}
我下载了http://search.maven.org/remotecontent?filepath=junit/junit/4.11/junit-4.11.jar,并将其保存到/home/tim/program_files/programming/java/junit-4.11.jar
。
仍然得到错误:
$ javac -cp "/home/tim/program_files/programming/java/junit-4.11.jar" MyTest.java
gcj-4.6: warning: already-compiled .class files ignored with -C
MyTest.java:4: error: The import org.junit cannot be resolved
import org.junit.runner.JUnitCore;
^^^^^^^^^
MyTest.java:5: error: The import org.junit cannot be resolved
import org.junit.runner.Result;
^^^^^^^^^
MyTest.java:6: error: The import org.junit cannot be resolved
import org.junit.runner.notification.Failure;
^^^^^^^^^
3 problems (3 errors)
我该怎么办?谢谢。
答案 0 :(得分:1)
使用命令
javac -cp "/home/tim/program_files/programming/eclipse/plugins" MyTest.java
java查找calsses(.class文件)而不是指定的direcotry中的jar文件,这不是jar文件添加到类路径的方式。你应该像这样指定:
javac -cp "/home/tim/.../plugins/org.junit_4.11.0.v201303080030/junit.jar" MyTest.java
但是,我不建议使用那个,因为它来自eclipse安装,我会下载它并把它放到另一个位置并使用它。
如果您有一个eclipse项目并希望将其添加到classpath,那么只需执行
right click on your project > Build Path > Configure build path > Libraries tab
然后点击add library
,选择JUnit
,然后选择版本并点击完成。