junit找不到课程 [英] junit cannot find class

查看:129
本文介绍了junit找不到课程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. 我能够运行junit附带的示例测试(从任何目录).我认为这表明我安装的junit非常好.

  1. 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):

java org.junit.runner.JUnitCore parent.child.fileName,我得到以下信息: JUnit版本4.1找不到类:parent.child.fileName时间:0 确定(0个测试)

java org.junit.runner.JUnitCore parent.child.fileName, I get the following: JUnit version 4.1 Could not find class: parent.child.fileName Time: 0 OK (0 tests)

  1. 即使我从命令中完全删除了parent.child,也没有任何区别.

  1. 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找不到课程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆