描述
我有 3 个程序。
我的主程序名为Main
。然后我有 2 个 jar
文件:Utils1
和 Utils2
。
Utils2
用于Utils1
。所以我用 Maven 将其导入到 Utils1
pom.xml
中,如下所示。
<dependency>
<groupId>org.group.project</groupId> // random text
<artifactId>Project</artifactId> // random text
<version>1.0.0</version> // random version
<scope>system</scope>
<systemPath>${basedir}/Utils2.jar</systemPath>
</dependency>
Utils2
包含一个名为 custompack
的包、一个名为 MyMaths
的类和一个名为 MyAddition
的方法。
Utils1
包含一个名为 CustomMaths
的类和一个名为 CustomAddition
的方法。 CustomAddition
调用 Utils2.custompack.MyMaths.MyAddition
。
因此 Utils1
的代码如下所示:
public int CustomAddition (int item1, int item2) {
MyMaths mymaths = new MyMaths();
return mymaths.MyAddition(item1, item2);
}
问题
当我在 Main
程序中使用 CustomAddition
时,我遇到以下错误:
java.lang.NoClassDefFoundError: custompack/MyMaths
问题
我认为 Utils2
jar 未在 Utils1
jar 中正确导入。
你有什么想法吗?谢谢。
最佳答案
我假设 Utils1 和 Utils2 都是自定义构建 jar。
基于maven的工作原理,
- 您应该首先构建 Utils1 项目,该项目应将 Artifact/jar 复制到 Maven 存储库(默认为 C:\Users[account].m2\repository,否则检查 C:\Users[account].m2\settings.xml,以防万一被覆盖)。
- 构建 Utils2 项目并运行使用 Utils2 中的类的主程序
此外,由于这些是自定义模块,您应该添加如下依赖项 -
<dependency>
<groupId>org.group.project</groupId> // random text
<artifactId>Utils2</artifactId> // random text
<version>1.0.0</version> // random version
</dependency>
当您在构建/执行过程中有一个 jar 在 Maven 存储库中不可用并且需要从不同位置拉取时,应使用以下内容
<systemPath>${system_path}/system.jar</systemPath>
参见https://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
关于java - 本地jar在maven jar中没有正确导入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59901434/