在IntelliJ IDEA中将外部库添加到工件jar [英] Adding external library to artifact jar in IntelliJ IDEA

查看:689
本文介绍了在IntelliJ IDEA中将外部库添加到工件jar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆