Adding external library to artifact jar in IntelliJ IDEA(在 IntelliJ IDEA 中将外部库添加到工件 jar)
问题描述
如何在 IntelliJ IDEA 中将外部库添加到项目中,以便在构建工件时它仍然可以访问库中的类?
我从项目结构创建了一个新的 Jar 工件,然后将外部 JAR 添加到库中,然后在模块列表中检查它,最后将其添加到工件的输出中.这些都不起作用.当我构建并尝试运行我的应用程序时,它会引发错误:
线程main"中的异常 java.lang.NoClassDefFoundError: <试图使用的类的路径>
我错过了什么,或者我这样做完全错了吗?
这里有 2 个选项:
- 将依赖项提取到工件 jar 中,使应用程序成为具有所有依赖项的单个可执行 jar
- 通过
Manifest.MF
链接依赖的 jar 并将它们复制到应用程序主 jar 附近
我准备了一个示例项目来演示这两种方法:
How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
- extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
- link the dependent jars via the
Manifest.MF
and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into outsingle
and outlinked
directories.
Relevant configurations:
这篇关于在 IntelliJ IDEA 中将外部库添加到工件 jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 IntelliJ IDEA 中将外部库添加到工件 jar


基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 降序排序:Java Map 2022-01-01