Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use classes from other project with maven? (ClassNotFoundException and NoClassDefFoundError is thrown)

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.

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:

  • Configure build path -> Libaries -> add Class Folder -> B\target
  • Configure build path -> Libaries -> add External Class Folder -> B\target
  • Configure build path -> Projects -> add -> B

I 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

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.

Edit: 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>

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>

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)

When I run the Application normally I still get the ClassNotFoundException and NoClassDefError.

Edit 2: I have added

<scope>compile</scope>

When I run Maven build (clean compile), part of my building process looks like this:

[INFO] Building centwertAdmin 0.0.1-SNAPSHOT

[INFO] ------------------------------------------------------------------------

[WARNING] The POM for de.qufis:CentwertApp:war:0.0.1-SNAPSHOT is missing, no dependency information available

[INFO] ------------------------------------------------------------------------

[INFO] BUILD FAILURE

Then there is the errormessage:

[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]

like image 767
Stoney Avatar asked Nov 22 '15 15:11

Stoney


People also ask

Can we exclude a class from Maven dependency?

Since Maven resolves dependencies transitively, it is possible for unwanted dependencies to be included in your project's classpath. For example, a certain older jar may have security issues or be incompatible with the Java version you're using. To address this, Maven allows you to exclude specific dependencies.

What is classpath in Maven project?

The reported classpath consists of references to JAR files cached in local Maven repository. Even the JAR artifact of the project is referenced from local Maven cache and not from the /target directory one or the other might expect.


1 Answers

can u try to remove <packaging>war</packaging> from B? Maven's default packaging is jar, and this is what you need for B.

Plus, add B to the dependencies of A.

  • Build B project. use mvn clean install, that would install B's JAR+POM in your local repository (.m2).
  • Verify it is there... Then Maven will be able to find it, so you will not get the error "Could not find artifact de.qufis:CentwertApp"
  • Build A.

Usually, dependencies are of type JAR. If B must be a WAR, see this.

like image 80
OhadR Avatar answered Sep 27 '22 20:09

OhadR