尽管类位于构建路径上,Java 仍抛出 ClassNotFoundExceptions

发布于 2024-11-24 08:38:39 字数 956 浏览 1 评论 0原文

我有两个不同的java项目(为了简单起见,我将它们称为项目1和项目2)加载到eclipse中,项目1被添加到项目2的构建路径中。我已经导入了项目的src文件夹中的唯一包1 到项目 2 的测试类中,在该测试类的代码中,我对项目 1 中的类进行了简单的对象声明,如下所示:

ProjectOneClass object = new ProjectOneClass();

此代码编译时没有错误,编译器识别出这些类位于构建路径上。但是,当我通过 Junit4 将代码作为 Java 应用程序运行时,程序在遇到这些代码行时会抛出 ClassNotFoundExceptions。该代码应该将这些 ClassNotFoundException 打印到错误日志中,但由于某种原因没有打印任何内容。我使用的是一个简单的

try {
    ...
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

结构,所以我不知道为什么它没有打印到错误日志中。

JUnit 打印输出很简单:

Junit execution complete
Summary: 4 succeeded, 3 failed.

成功的四个没有引用导入的项目 1 包。

我尝试了对构建路径配置进行各种更改,但没有任何改进的迹象。我认为唯一可以解决此问题的是构建路径配置窗口的“顺序和导出”选项卡中指定的构建路径的顺序。现在的顺序是:

project 2 packages
EAR Libraries
JRE System Libraries
JUnit4
a few JAR files (c3p0, commons-codec, ojdbc6)
project 1

不过,我不确定问题是出在这里还是其他地方。如果有人能帮助我解决这个问题,我将非常感激。感谢您的阅读。

I have two different java projects (I'll call them project 1 and Project 2 for simplicity's sake) loaded into eclipse, and project 1 is added to the build path of project 2. I have imported the only package in the src folder of project 1 into a test class for project 2, and within the code of that test class I have simple object declarations of classes from project 1 as such:

ProjectOneClass object = new ProjectOneClass();

This code compiles without error, the compiler recognizes that these classes are on the build path. When I run the code as a Java application via Junit4, though, the program throws ClassNotFoundExceptions when it comes across these lines of code. The code is supposed to print these ClassNotFoundExceptions to the error log, but for some reason nothing is being printed. I'm using a simple

try {
    ...
} catch (ClassNotFoundException e) {
    e.printStackTrace();
}

structure, so I don't know why it's not printing to the error log.

The JUnit printout is simply:

Junit execution complete
Summary: 4 succeeded, 3 failed.

The four that succeed do not reference the imported project 1 package.

I have tried all manners of changes to the build path configurations, and nothing has shown any promise of improvement. The only thing I can think might be able to fix this is the order of the build path as specified in the Order And Export tab of the build path configuration window. Right now the order is:

project 2 packages
EAR Libraries
JRE System Libraries
JUnit4
a few JAR files (c3p0, commons-codec, ojdbc6)
project 1

I don't know for sure if the problem lies here or elsewhere, though. If anyone can help me out with this, I'd be very grateful. Thanks for reading.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

︶ ̄淡然 2024-12-01 08:38:39

我想通了这个问题。项目 1 依赖于项目 2 的构建路径中没有的几个 JAR。我认为由于项目 1 在其构建路径中设置了这些 JAR,因此它仍然可以正常工作。然而,在从项目 1 创建类的实例后,我发现项目 2 在其自己的构建路径中需要这些 JAR。

如果我实例化的类是这些 JAR 之一的类的实现/扩展,这对我来说是有意义的,但事实并非如此。

不管怎样,通过向项目 2 添加几个 JAR,我解决了这个问题。谢谢大家。

I figured out the issue. Project 1 relies on several JARs that Project 2 doesn't have as part of its build path. I figured that since Project 1 had those JARs set within its build path, it would still work fine. Upon creating instances of classes from Project 1, however, I found that Project 2 required these JARs in its own build path.

This would make sense to me if the classes I was instantiating were implementations/extensions of a class from one of these JARs, but they weren't.

Regardless, by adding a couple of JARs to Project 2 I fixed the problem. Thanks everyone.

泅人 2024-12-01 08:38:39

您应该为您的项目 2 类文件设置运行配置。

在 Eclipse 中,您可以右键单击 -- 选择运行方式 -- 运行配置,然后在类路径选项卡中,确保通过单击添加项目添加项目 1。

You should set the Run Configuration for your Project 2 class file.

In Eclipse, you can right click -- Select Run As -- Run Configuration and then in the classpath tab, make sure Project 1 is added by clicking on Add Projects.

無心 2024-12-01 08:38:39

让我们缩小问题范围,看看它是否与 JUnit 启动配置相关,或者涉及两个 Eclipse 项目的更常见问题。

在项目 2 中使用 main(String[]) 方法创建一个类。在该方法中,引用项目 1 中似乎导致 CNFE 的一种或多种类型。使用 Java 应用程序启动配置类型运行此类。 (我从您的帖子中假设您在基于“JUnit Test”启动配置类型的启动时遇到问题。)报告回来。

另外,我最近在 JUnit 4 测试中使用 JUnit Test 启动配置类型时看到了一些有趣的行为,尽管我不认为它完全被破坏了。所以你可以尝试 JUnit 3 配置。

Let's narrow down the problem and see if it relates to a JUnit launch config, or a more general problem involving the two Eclipse projects.

Create a class in Project 2 with a main(String[]) method. In that method, reference one or more of the types in Project 1 that appear to be causing your CNFE's. Run this class using the Java Application launch configuration type. (I'm assuming from your post that you're having trouble with launches based on the "JUnit Test" Launch Configuration type.) Report back.

Also, I've seen some funny behavior using the JUnit Test launch configuration type with JUnit 4 tests of late, although I don't think it's totally broken. So you could try a JUnit 3 configuration.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文