简体   繁体   English

引用项目中某个类的 java.lang.NoClassDefFoundError

[英]java.lang.NoClassDefFoundError for a class within a referenced project

In my project named Bookyard.Server , I reference another project of mine named Bookyard.Contracts .在我名为Bookyard.Server 的项目中,我引用了另一个名为Bookyard.Contracts 的项目。

In Eclipse for Java EE, Mars 2, I set the Build Path and Project References for the Bookyard.Server project like so:在 Eclipse for Java EE, Mars 2 中,我为Bookyard.Server项目设置了构建路径项目引用,如下所示

在此处输入图片说明

在此处输入图片说明

I also added the bin folder of the Bookyard.Contracts project to the %CLASSPATH% like so:我还将Bookyard.Contracts项目的bin文件夹添加到%CLASSPATH%如下所示:

在此处输入图片说明

However, when I debug my Bookyard.Server project, at the line where it references the contracts project, it raises a java.lang.NoClassDefFoundError exception for practice/bookyard/contracts/Constants .但是,当我调试Bookyard.Server项目时,在它引用合同项目的那一行,它会引发一个java.lang.NoClassDefFoundError异常,用于practice/bookyard/contracts/Constants

Bookyard.Server书院服务器

import practice.bookyard.contracts.Constants;
...
if (!body.get("sub").toString()
       .contentEquals(Constants.JWT_SUBJECT_LOGIN_REQUEST))

As there is dependency among the projects you need to add your Bookyard.Contracts.由于项目之间存在依赖关系,因此您需要添加 Bookyard.Contracts。 under Deployment Assembly section of project properties在项目属性的部署程序集部分下

I got the NoClassDefFoundError after some reorganizing of my Eclipse 2020-12 workspace in which I have 8 cooperating projects.在对我有 8 个合作项目的Eclipse 2020-12工作区进行了一些重组后,我得到了NoClassDefFoundError The projects build upon (ie depend on) one another, and I had entered the dependencies using Package Explorer -> right click on project name -> Build Path -> Configure Build Path ... -> "Projects" Tab -> "Add..." Button.这些项目相互建立(即依赖),我已经使用包资源管理器输入了依赖项 -> 右键单击​​项目名称 -> 构建路径 -> 配置构建路径 ... ->“项目”选项卡 ->“添加……”按钮。

Background: Because I ran into so many problems with the Java Module System, I decided to avoid it altogether and continue to use Java 1.8 even though I have (don't ask me why) JavaSE-13 (also shown as "JRE 13") as my runtime.背景:因为我在 Java 模块系统方面遇到了很多问题,所以我决定完全避免它并继续使用 Java 1.8,即使我有(不要问我为什么)JavaSE-13(也显示为“JRE 13”) ) 作为我的运行时。 Now back to my observations and finally the solution:现在回到我的观察,最后是解决方案:

Observation 1: Some of my projects were ticked "Enable project specific settings" and others were not.观察 1:我的一些项目被勾选了“启用项目特定设置”,而其他项目则没有。

Observation 2: In the "Java Build Path" dialog -> Tab "Projects" I noticed that the information about the "Required projects on the build path" was not consistent.观察 2:在“Java 构建路径”对话框 -> 选项卡“项目”中,我注意到有关“构建路径上的必需项目”的信息不一致。 The same lower-level project was shown as "Is modular" on some projects that used it, and as "Is not modular" on other projects.同一个较低级别的项目在使用它的一些项目中显示为“是模块化的”,而在其他项目中显示为“不是模块化的”。 (To see this "modular or not" info about a required project, expand its line by clicking on the > in front of the name). (要查看有关所需项目的“模块化与否”信息,请通过单击名称前面的 > 展开其行)。

Solution (part 1): For all my projects in the workspace I made sure they did NOT have a tick in "Enable project specific setting" in the dialog "Java Compiler" of the project properties.解决方案(第 1 部分):对于工作区中的所有项目,我确保它们在项目属性的“Java 编译器”对话框中的“启用项目特定设置”中没有勾选。 So that all of them should now share the Workspace Settings for "Java Compiler".这样他们现在都应该共享“Java Compiler”的工作区设置。

Solution (part 2): In the list of the "required projects" of each project I removed the entries for the projects that were listed as "Is modular" and immediately added them again.解决方案(第 2 部分):在每个项目的“必需项目”列表中,我删除了列为“模块化”的项目的条目,并立即再次添加它们。 After this operation, all required projects of all my 8 projects were listed as "Is not modular".这次操作后,我所有的8个项目的所有必需项目都被列为“不是模块化”。

Effect: Now the software runs fine again.效果:现在软件又可以正常运行了。

Hypothesis 1: Maybe the class loaders use different search mechanisms when they look for class files in projects, depending on whether the USING project declares the projects it uses as modular or not.假设1:可能类加载器在查找项目中的类文件时使用了不同的搜索机制,这取决于USING项目是否将其使用的项目声明为模块化。

Hypothesis 2: Maybe Eclipse COPIES the "is modular" information of a lower-level project when it adds it to the "required projects" of another project.假设 2:Eclipse 可能会在将较低级别项目的“模块化”信息添加到另一个项目的“必需项目”时复制它。 With the effect that if the "is modular" information changes, the depending projects are out of sync.结果是,如果“模块化”信息发生变化,依赖的项目就会不同步。 If this is true, Eclipse should just REFER to the "is modular" information receive an event in case it changes.如果这是真的,Eclipse 应该只是参考“模块化”信息接收事件,以防它发生变化。

My opinion: I regard this is an error.我的意见:我认为这是一个错误。 Why?为什么? A project which requires another one should not depend on whether the other project is "modular" or not.一个需要另一个项目的项目不应依赖于另一个项目是否“模块化”。

Sometimes you get NoClassDefFoundError if your class has an uncaught Exception in constructor or static block of your class.有时,如果您的类在您的类的构造函数或静态块中有未捕获的异常,您会得到 NoClassDefFoundError。 The main error is suppressed by NoClassDefFoundError. NoClassDefFoundError 抑制了主要错误。

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

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