问题描述
-
我能够运行junit附带的示例测试(从任何目录).我认为这表明我安装的junit非常好.
I am able to run the sample tests supplied with junit (from any directory). I would think that this suggests my installation of junit is perfectly fine.
我假设要编译一个junit测试,它与任何其他Java文件都没有什么不同,即:javac fileName.java
I assume, to compile a junit test, it is no different from any other java file, namely: javac fileName.java
我的测试文件(.java和生成的.class)位于:c:\ parent \ child.显然,为了编译.java文件,我在第一行有一个package语句:package parent.child,其后是最重要的:import junit.framework.TestCase;.此后,有一个公共类fileName定义扩展了TestCase {}.该文件编译时没有任何警告或错误.
My test file (.java and resulting .class) lives in: c:\parent\child. Obviously, in order to compile the .java file, I have a package statement on the first line: package parent.child followed by the all-important: import junit.framework.TestCase; After this, there is a public class fileName definition extends TestCase {}.The file compiles without any warnings or errors.
当我尝试时(在存在FileName的c:\ parent \ child目录中,.java和.class均如此):
when I attempt (in the c:\parent\child directory where fileName exists, both the .java and .class):
-
即使我从命令中完全删除了parent.child,也没有任何区别.
Even if I drop parent.child altogether from the command, it makes no difference.
我的CLASSPATH环境变量设置为:c:\ parent \ junit \ junit-4.1.jar; c:\ parent \ junit;.
My CLASSPATH environment variable is set to:c:\parent\junit\junit-4.1.jar;c:\parent\junit;.
如果我尝试使用-cp c:\或c:\ parent \ child或其他任何东西运行,我仍然会收到错误消息.
If I trying running with -cp c:\ or c:\parent\child or anything else, I still get the error.
推荐答案
Java包名称实际上是类名称的一部分,并且还以特定方式用于查找*.class
文件.如果Java要查找名为parent.child.fileName
的类,它将查找名为parent/child/fileName.class
的文件-即,它将在名为parent
的目录中的名为child
的目录中查找该文件.您必须指定类路径,以便找到parent
目录,例如
Java package names are actually part of the class name, and they're also used in a specific way to find *.class
files. If Java wants to find a class named parent.child.fileName
, it's going to look for a file named parent/child/fileName.class
-- i.e., it's going to look for the file in a directory named child
in a directory named parent
. You have to specify the class path such that the parent
directory will be found, something like
java -cp "c:/junit/junit.jar;c:/" org.junit.runner.JUnitCore parent.child.fileName
类路径(-cp)参数必须指定Java应该查找的所有类的位置,包括像junit.jar
这样的jar文件,我想象它位于名为junit
的目录中.分号;"用于分隔类路径上的条目(假设您使用的是Windows;在实际计算机上,它是一个:".)类路径中的"c:/"条目是用于查找parent
目录,然后是您的课程.
The class path (-cp) argument has to specify all the places that Java should look for classes, including jar files like junit.jar
, which I've imagined is located in a directory called junit
. The semicolon ";" is used to separate entries on the class path (assuming you're using Windows; on real computers it's a ":" instead.) The "c:/" entry in the class path is the one that will be used to find the parent
directory and thus your class.
这篇关于junit找不到课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!