简体   繁体   English

使用eclipse,java maven项目可以编译,但是在运行时会给出错误

[英]using eclipse, java maven project compiles but gives error when running

There are a large number of answers to the error I'm getting, but each of the solutions that I've tried (that I understand at least) have not solved my problem. 我得到的错误有很多答案,但是我尝试过的每个解决方案(至少我理解)都没有解决我的问题。
My project layout in Eclipse looks like this: 我在Eclipse中的项目布局如下所示:

MavenProject1 Maven项目1

src src

resources 资源

etc. 等等

MavenProject2 MavenProject2

src src

resources 资源

etc. 等等

MavenProjectX MavenProjectX

src src

resources 资源

etc. 等等

Each project compiles and runs fine. 每个项目都可以编译并正常运行。 But what I'm trying to do is use one of the projects, say some of the code in MavenProject2, in MavenProject1. 但是我想做的是使用其中一个项目,例如MavenProject2中的MavenProject1中的一些代码。 I've updated the build path and information so that I can import MavenProject2 into MavenProject1 and reference the methods I want. 我已经更新了构建路径和信息,以便可以将MavenProject2导入MavenProject1并引用所需的方法。
The reference to the MavenProject2 method in MavenProject1 enumerates and doesn't give an error in the editor (ie com.MavenProject2.Method()), and MavenProject1 compiles. 在MavenProject1中对MavenProject2方法的引用枚举并且在编辑器中没有给出错误(即com.MavenProject2.Method()),并且MavenProject1进行了编译。 But when I try to run it, I get the error below. 但是,当我尝试运行它时,出现以下错误。 I've tried fixing the classpath (as I understand it, which could be wrong), I've tried adding the project, external jar, just about every option, in the run configuration for the project, but I keep hitting the same error on line in MavenProject1 that calls the method in MavenProject2. 我试过修复类路径(据我所知,这可能是错误的),我试过在项目的运行配置中添加项目,外部jar,几乎所有选项,但我一直遇到相同的错误在MavenProject1行调用MavenProject2的方法。

[WARNING]  java.lang.reflect.InvocationTargetException  at
> sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)   at
> sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)   at
> sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)   at
> java.lang.reflect.Method.invoke(Unknown Source)   at
> org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)  at
> java.lang.Thread.run(Unknown Source) Caused by:
> java.lang.NoClassDefFoundError: com/MavenProject2/Method_From2    at
> com.MP1.MP1.main(CAPI.java:154)   ... 6 more Caused by:
> java.lang.ClassNotFoundException: com.MavenProject2.Method_From2

When you first build the projects that your current project depends on, make sure they are stored in your local maven repository. 首次构建当前项目所依赖的项目时,请确保将它们存储在本地Maven存储库中。 Then when you add those as dependencies to your project's pom.xml this should hopefully resolve it for the build. 然后,当将它们作为依赖项添加到项目的pom.xml中时,这应该可以解决该问题。

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

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