简体   繁体   English

cmdline中的Junit-java.lang.ClassNotFoundException

[英]Junit from cmdline - java.lang.ClassNotFoundException

I have a junit test suite that I have running successfully from eclipse. 我有一个从eclipse成功运行的junit测试套件。 When I try to run the jar with 当我尝试使用

java -jar AppTest.jar

I get the following: 我得到以下内容:

Exception in thread "main" java.lang.NoClassDefFoundError: org/junit/runner/JUnitCore
at appdirect.AppTest.main(AppTest.java:18)
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:247)
... 1 more

I know the jar was created successfully since I am creating the logs (programmatically from AppTest) and echoing $PATH prints the directory that the junit.jar is in. 我知道jar是成功创建的,因为我正在创建日志(通过AppTest以编程方式),并且回显$ PATH会打印出junit.jar所在的目录。

How do I ensure when I am jarring in eclipse I am picking up all the junit jars? 如何确保当我在日食震撼时能捡起所有的junit罐子?

You need to add the junit jar to the classpath , not your system's path. 您需要将junit jar添加到classpath ,而不是系统的路径。

One way to do this is to set the classpath to point to junit.jar and AppTest.jar and invoke your main. 一种方法是将类路径设置为指向junit.jar和AppTest.jar并调用您的main。

java -cp /path/to/junit.jar:/path/to/AppTest.jar com.example.Main

Path might print you the jar's but is the jars there in the classpath. 路径可能会打印出罐子,但是罐子在类路径中。 There is one more environment variable called CLASSPATH in which you need to append your junit jars and try running your AppTest.jar 还有一个名为CLASSPATH环境变量,您需要在其中添加附加的junit jar并尝试运行AppTest.jar

How to set classpath 如何设置类路径

在主Jar的清单中添加对另一个Jar的引用。

Believe it or not, it was as simple as making sure I exported as a "runnable Jar" in the wizard and used the "Extract required libraries..." option. 信不信由你,这就像确保我在向导中导出为“可运行的Jar”并使用“提取所需的库...”选项一样简单。 I had been trying to export it as a normal jar. 我一直试图将其导出为普通的jar。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM