【发布时间】:2015-08-19 11:34:50
【问题描述】:
我创建了一个 maven 基础项目,它有两个模块,A 和 B。在A 中,我添加了B 作为依赖项,还通过Project Structure 在B 中添加了来自maven 的JSoup。
现在我想运行以下代码 答:
public class A {
public static void main(String args[]){
new B().foo();
}
}
乙:
public class B {
public void foo() {
System.out.println("B....");//prints B...
new Document("S");// NoClassDefFoundError exception
}
}
当我运行以下代码时,它会说:
Exception in thread "main" java.lang.NoClassDefFoundError: org/jsoup/nodes/Document
Jsoup 仅添加到 B.iml 而非 B.pom 文件中:
<orderEntry type="library" exported="" name="org.jsoup:jsoup:1.8.3" level="project" />
我可以通过将 jsoup 依赖项添加到 A 来解决这个问题,但我认为这不是一个好主意。
有什么方法可以让A 识别所有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">
<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 的绒球
<?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>
【问题讨论】:
-
你试过
File -> Invalidate Cache\Restart -> Invalidate and Restart -
使用maven时,依赖是在pom中声明的,别处没有
-
@KarthikeyanVaithilingam 是,但仍然出现异常
-
@Salvador 你能发布你的 pom.xml(s) 和模块结构吗?
-
AFAIK,这是另一种方式,您将 maven 项目导入 intellij,因此项目文件(iml 等)从 pom.xml 更新
标签: java maven intellij-idea