Intellij - maven module dependecy. Error NoClassDefFound

I created a basic maven project and has two modules: A and B In A I added B as a dependency, and also added JSoup from maven to B through Project Structure .

Now I want to run the following codes A:

 public class A { public static void main(String args[]){ new B().foo(); } } 

IN:

 public class B { public void foo() { System.out.println("B....");//prints B... new Document("S");// NoClassDefFoundError exception } } 

When I run the following code, it says:

 Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/nodes/Document 

Jsoup is only added to the B.iml not B.pom :

 <orderEntry type="library" exported="" name="org.jsoup:jsoup:1.8.3" level="project" /> 

I can solve this problem by adding jsoup dependency to A , but I think this is not a good idea.

Is there a way that A recognizes all the dependencies of B ?

Root root:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>xxx</groupId> <artifactId>xxx</artifactId> <packaging>pom</packaging> <version>0.5-SNAPSHOT</version> <modules> <module>A</module> <module>B</module> </modules> </project> 

A pom

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>xxx</artifactId> <groupId>xxx</groupId> <version>0.5-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>A</artifactId> </project> 

B pom:

 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>xxx</artifactId> <groupId>xxx</groupId> <version>0.5-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>B</artifactId> </project> 
+4
source share
2 answers

In A, I added B as a dependency

Therefore, you need to add this dependency to the A pom file too

  <dependencies> <dependency> <groupId>xxx</groupId> <artifactId>B</artifactId> <version>0.5-SNAPSHOT</version> </dependency> </dependencies> 

and also added JSoup from maven to B

You should also add this dependency to the B pom file.

  <dependencies> <dependency> <groupId>org.jsoup</groupId> <artifactId>jsoup</artifactId> <version>1.8.3</version> </dependency> </dependencies> 

through the project structure

The management of IntelliJ and Maven is not dependent on each other.

+1
source

You did not add B as a dependency on A. To do this, you must add A pom.xml to this line.

 <dependencies> <dependency> <groupId>xxx</groupId> <artifactId>B</artifactId> <version>0.5-SNAPSHOT</version> </dependency> </dependencies> 

Also, jsoup is not defined as a dependency in any projects. You should learn more about maven and dependency management .

0
source

All Articles