本文介绍了我如何在Maven中使用其他项目中的类? (引发ClassNotFoundException和NoClassDefFoundError)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有两个Vaadin项目(我将其命名为A和B)。两者都使用Maven来解决依赖关系,并且都在同一工作区中。我正在使用Eclipse,并且正在使用m2e插件。我想在项目A中使用B的某些类。在Eclipse中,我可以对其进行实例化,而不会出现错误/警告,但是当我尝试运行AI时,会得到 ClassNotFoundException 和 NoClassDefFoundError 是由于B类的实例化而造成的。I have two Vaadin projects (I will name them A and B). Both are using Maven for resolving the dependencies and are in the same workspace. I'm working with Eclipse and I'm using the m2e-plugin. I want to use some classes of B in project A. In Eclipse I can instatiate them without errors/warnings but when I try to run A I get a ClassNotFoundException and a NoClassDefFoundError caused by instatiating a class of B. .class A的文件位于 ... \workspace\A\target\classes 中,而项目B的文件位于中。 .\workspace\B\target\classs 。 我一直在尝试解决此问题,但没有找到解决方案。 到目前为止,我尝试过的操作:The .class files of A are located in ...\workspace\A\target\classes and the for project B they are in ...\workspace\B\target\classes.I've been trying to solve this problem but I didn't find a solution.What I tried so far: 配置构建路径->库->添加类文件夹-> B\target 配置构建路径->库->添加外部类文件夹-> B\target 配置构建路径->项目->添加-> B我认为添加项目是必要的,因为当我删除项目时,当我尝试使用以下项目中的类时,Eclipse会给我错误消息B in AI think adding the project is necessary because when I remove it, Eclipse gives me error messages when I try to use the classes from B in A我不知道该怎么办。也许我必须配置 pom.xml 文件,但是我不知道该怎么做。I don't have any idea what else to do. Maybe I have to configure my pom.xml file but I don't know what I must do there.编辑:在项目B的pom.xml中:In pom.xml of project B:<groupId>de.qufis</groupId><artifactId>CentwertApp</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>CentwertApp</name>在项目A的pom.xml中:In pom.xml of project A: <dependency> <groupId>de.qufis</groupId> <artifactId>CentwertApp</artifactId> <version>0.0.1-SNAPSHOT</version> <type>war</type> <scope>import</scope> </dependency>我确实使用maven build清理了项目B。 然后我尝试对项目A进行相同操作,但随后出现错误消息:在vaadin-addons中找不到工件de.qufis:CentwertApp:war:0.0.1-SNAPSHOT( http://maven.vaadin.com/vaadin-addons )I did clean compile for project B with maven build.Then I tried to do the same with project A but then I get error message:Could not find artifact de.qufis:CentwertApp:war:0.0.1-SNAPSHOT in vaadin-addons (http://maven.vaadin.com/vaadin-addons)正常运行应用程序时,我仍然会收到ClassNotFoundException和NoClassDefError。When I run the Application normally I still get the ClassNotFoundException and NoClassDefError.编辑2:我已经添加了Edit 2:I have added<scope>compile</scope>当我运行Maven build(干净编译)时,部分构建过程如下:When I run Maven build (clean compile), part of my building process looks like this: [INFO] Building centwertAdmin 0.0.1-SNAPSHOT[INFO] Building centwertAdmin 0.0.1-SNAPSHOT [INFO] --------- -------------------------------------------------- -------------[INFO] ------------------------------------------------------------------------ [警告] de.qufis:CentwertApp:war:0.0.1-SNAPSHOT的POM丢失,没有可用的依赖项信息[WARNING] The POM for de.qufis:CentwertApp:war:0.0.1-SNAPSHOT is missing, no dependency information available [INFO] ---------------------------- --------------------------------------------[INFO] ------------------------------------------------------------------------ [信息]内置故障然后出现错误消息: [错误]无法在项目centwertAdmin上执行目标:无法解析项目de.qufis:centwertAdmin:war:0.0.1-SNAPSHOT的依赖项:无法在 http://maven.vaadin.com/vaadin-addons 已缓存在本地存储库中,解析不会重新尝试直到t他的vaadin-addons更新间隔已经过去或强制进行了更新-> [帮助1][ERROR] Failed to execute goal on project centwertAdmin: Could not resolve dependencies for project de.qufis:centwertAdmin:war:0.0.1-SNAPSHOT: Failure to find de.qufis:CentwertApp:war:0.0.1-SNAPSHOT in http://maven.vaadin.com/vaadin-addons was cached in the local repository, resolution will not be reattempted until the update interval of vaadin-addons has elapsed or updates are forced -> [Help 1]推荐答案您可以尝试删除< packaging> war< / packaging> ? Maven的默认包装为 jar ,这就是您需要B的条件。can u try to remove <packaging>war</packaging> from B? Maven's default packaging is jar, and this is what you need for B.此外,将B添加到依存关系。 构建B项目。使用 mvn全新安装,它将在您的本地存储库(.m2)中安装B的 JAR + POM。 验证它是否存在...然后 Maven将能够找到它,所以您将不会收到错误消息可能找不到工件de.qufis :CentwertApp Build A。Build B project. use mvn clean install, that would install B'sJAR+POM in your local repository (.m2).Verify it is there... ThenMaven will be able to find it, so you will not get the error "Couldnot find artifact de.qufis:CentwertApp"Build A.通常,依赖项的类型为JAR。如果B 必须为WAR,请参见此。Usually, dependencies are of type JAR. If B must be a WAR, see this. 这篇关于我如何在Maven中使用其他项目中的类? (引发ClassNotFoundException和NoClassDefFoundError)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-25 22:51
查看更多