简体   繁体   English

Eclipse在我的/ test源文件夹中的类路径上找不到类

[英]Eclipse can't find classes on the classpath in my /test source folder

I'm trying to write some tests on my Java source code in Eclipse. 我正在尝试在Eclipse的Java源代码上编写一些测试。 I created a new Java source directory called /test . 我创建了一个名为/test的新Java源目录。

I'm writing JUnit 4 tests on my code. 我正在我的代码上编写JUnit 4测试。 However, when I run my code, I sometimes get really weird errors due to things missing from the classpath evidently: 但是,当我运行代码时,有时会由于类路径中显然缺少的东西而得到一些奇怪的错误:

java.lang.NoClassDefFoundError: org/apache/commons/digester/Digester
    at net.sf.jasperreports.engine.JasperCompileManager.compileReportToStream(JasperCompileManager.java:169)
    at com.smartsports.sswebserver.service.ReportGeneratorServiceImpl.compile(ReportGeneratorServiceImpl.java:94)
    at com.smartsports.sswebserver.service.ReportGeneratorServiceImplTestCase.testCompile(ReportGeneratorServiceImplTestCase.java:75)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:616)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.digester.Digester
    at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
    ... 27 more

Everything else seems to run and compile great, but tests fail on stuff like this all the time. 其他所有东西似乎都可以运行和编译,但是测试始终在这种情况下失败。 What am I doing wrong? 我究竟做错了什么?

Looks like you are using Jasper reports, and since your code directly references that and that Jar file is included in your classpath, it compiles fine. 看起来您正在使用Jasper报告,并且由于您的代码直接引用了该报告,并且该Jar文件包含在您的类路径中,因此可以很好地进行编译。 However, Jasper reports appears to depends on the Apache Digester stuff which also needs to be in your classpath. 但是,Jasper报告似乎取决于Apache Digester的内容,这些东西也需要放在您的类路径中。 Get the Jar file for the Apache Digester and add it to your classpath. 获取Apache Digester的Jar文件并将其添加到您的类路径中。

You will likely hit this with many Jar files as Jasper reports probably depends on a lot of things, so you can either find each one as it fails or find all of the Jars by looking in the Jasper reports distribution and you will probably find a directory that contains all of the required Jars. 您可能会用许多Jar文件来实现此目的,因为Jasper报告可能取决于很多事情,因此您可以找到每一个失败的Jasper报告,也可以通过在Jasper报告分发中查找找到所有Jars,您可能会找到一个目录包含所有必需的Jar。

If you are doing plugin development, the usual way to accomplish this is to create a plugin that contains Jasper reports and all of its required Jar files (in fact you might find an OSGi bundle (== plugin) out there that has this). 如果要进行插件开发,通常的方法是创建一个包含Jasper报告及其所有必需的Jar文件的插件(实际上,您可能会在其中找到具有此功能的OSGi捆绑包(==插件))。

If you are not doing plugin development, then configure your Java Build Path (select the project, right-click and select Build Path -> Configure Build Path) and put the required Jar files in the Libraries tab using one of the many options there. 如果您不进行插件开发,则配置Java Build Path(选择项目,右键单击并选择Build Path-> Configure Build Path),然后使用其中的多个选项之一将所需的Jar文件放在Libraries选项卡中。

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

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