问题描述
如何在IntelliJ IDEA中的项目中添加外部库,以便在构建工件时,它仍然可以访问库中的类?
我已经从Project Structure创建了一个新的Jar工件,然后将外部JAR添加到Libraries,然后在Modules List中进行检查,最后将其添加到Output为神器。这些都不行。当我构建并尝试运行我的应用程序时,它会抛出一个错误:
线程main中的异常java.lang.NoClassDefFoundError:< path的课程试图使用>
我缺少什么,或者我这样做完全错了?
您有两个选项:
- 工件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 out\single
and out\linked
directories.
Relevant configurations:
这篇关于在IntelliJ IDEA中将外部库添加到工件jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!