简体   繁体   中英

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:

MavenProject1

src

resources

etc.

MavenProject2

src

resources

etc.

MavenProjectX

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. I've updated the build path and information so that I can import MavenProject2 into MavenProject1 and reference the methods I want.
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. 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.

[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. Then when you add those as dependencies to your project's pom.xml this should hopefully resolve it for the build.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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