使用 jar 文件从命令行运行 junit
我有两个java文件,Point.java和PointTest.java。在 Eclipse 中工作正常,但我试图通过命令行编译它们
我将 junit 4.8.2 jar 文件下载到与 .java 文件相同的目录中
我目前正在尝试使用此命令来编译它们,但我收到错误
javac -cp junit-4.8.2.jar:. Point.java PointTest.java
我已经尝试过也可以使用 -cp 命令。第一个错误是
package org.junit does not exist
我在这里做错了什么?如果我将 jar 文件提取到目录中,那么我就可以正确编译 java 文件。
如果有帮助,测试文件中的前几个导入声明是
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
I have two java file, Point.java and PointTest.java. The work fine in eclipse but I'm trying to compile them through commandline
I downloaded the junit 4.8.2 jar file into the same directory as the .java files
I am currently trying this command to compile them but im getting errors
javac -cp junit-4.8.2.jar:. Point.java PointTest.java
I have tried the command with -cp as well. The first error is
package org.junit does not exist
What am I doing wrong here? If I extract the jar file in the directory, then I am able to compile the java files correctly.
In case it helps, the first few import declarations in the test file are
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您能否验证 junit jar 的名称是否正确。默认名称是 junit-4.8.2.jar 而不是 junit.4.8.2.jar
Could you verify if the name of the junit jar is correct. The default name is junit-4.8.2.jar and not junit.4.8.2.jar
使用
-verbose
标志进行编译,以便您可以看到编译器正在做什么。它将打印出类路径。Compile with the
-verbose
flag so you can see what the compiler is doing. It will print out the classpath.我建议查看 javac 页面中的示例,特别是 分离源文件和类文件。拆分源代码、类文件和库是很常见的。例如,请参阅 Maven 标准目录布局< /a> 和 Sun 开发人员网络 - 项目约定。大多数 IDE 都会鼓励类似的布局。
为什么不做简单的事情并将源文件与类文件放在同一目录中呢?一方面,如果您删除或重命名一个类,但忘记删除该类文件,您可以拥有引用旧类的源代码。还有一些我们大多数人都不记得的其他问题,因为没有人将类和源文件放在同一目录中。
如果您只是尝试使用 Java 和 JUnit,我强烈推荐像 Eclipse 这样的 IDE。对于自动化构建,您可能需要考虑使用 Ant、Maven 或 Ivy,但对于入门来说,IDE 的学习曲线较小,但有很多额外的好处(代码完成、调试、用于检查 JUnit 测试失败的 UI...) 。
I suggest looking at the examples in the
javac
page, particularly Separating Source Files and Class Files. Splitting out source, class files and libraries is common. See, for example, the Maven Standard Directory Layout and Sun Developers Network - Project Conventions. Most IDEs will encourage a similar layout.Why not do the simple thing and have source files in the same directory as class files? For one thing, if you delete or rename a class, but forget to delete the class file, you can have source code that references the old class. There are a few other gotchas that most of us don't remember because no one puts classes and source files in the same directory.
If you are just experimenting with Java and JUnit I strongly recommend an IDE like Eclipse. For automated builds you might want to consider looking into Ant, Maven or Ivy, but for getting started an IDE has a smaller learning curve with a lot of extra benefits (code completion, debugging, a UI for reviewing JUnit test failures...).