java 如何在我自己的项目 JAR 中包含外部 JAR

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3728705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 03:11:49  来源:igfitidea点击:

How do I include external JARs in my own Project JAR

javaexecutable-jar

提问by Wilko

I have a Java application and created a JAR file and deployed it.

我有一个 Java 应用程序并创建了一个 JAR 文件并部署了它。

The App uses external JARs such as the Log4J JAR. When creating my JAR file, how do I include all external dependent JARs into my archive?

该应用程序使用外部 JAR,例如 Log4J JAR。创建 JAR 文件时,如何将所有外部依赖 JAR 包含到我的存档中?

In order to get my App working, I'm having to copy the Log4J JAR into the same directory as my own JAR which kinda defeats the purpose of the jar. Wouldn't it be more elegant to have 1 single JAR file to deploy?

为了让我的应用程序正常工作,我必须将 Log4J JAR 复制到与我自己的 JAR 相同的目录中,这有点违背了 jar 的目的。部署 1 个单独的 JAR 文件不是更优雅吗?

回答by coder623

If you use Eclipse, You can extract all included files into one runnable jar like this:

如果您使用 Eclipse,您可以将所有包含的文件提取到一个可运行的 jar 中,如下所示:

  1. Right click on your project name from Package Explorer and select Export.
  2. In Export screen, select Java -> Runnable JAR file and Next.
  3. Fill in the Runnable JAR File Spec screen and Finish.
  1. 从包资源管理器中右键单击您的项目名称,然后选择导出。
  2. 在导出屏幕中,选择 Java -> Runnable JAR 文件,然后选择下一步。
  3. 填写 Runnable JAR File Spec 屏幕并完成。

You can choose whether to package dependency jars as individual jar files or extract them into the generated JAR.

您可以选择是将依赖 jar 打包为单独的 jar 文件还是将它们解压到生成的 JAR 中。

回答by Pascal Thivent

You could use something like One-JARto package your Java application together with its dependency into a single executable Jar file (One-JAR uses a custom classloader to make JARs nesting possible).

您可以使用类似One-JAR 的工具将 Java 应用程序及其依赖项打包到单个可执行 Jar 文件中(One-JAR 使用自定义类加载器使 JAR 嵌套成为可能)。

回答by Mike Baranczak

You have to expand the library jars into the same place where your compiled classes go, then make a jar from that. Depending on how your build process is set up, there may be multiple ways to achieve this. It's not rocket science - a jar is just a zip archive with a META-INF directory at the root level.

您必须将库 jar 扩展到编译类所在的同一位置,然后从中制作一个 jar。根据构建过程的设置方式,可能有多种方法可以实现这一点。这不是火箭科学——jar 只是一个 zip 存档,在根级别有一个 META-INF 目录。

回答by Ashish Patil

Keeping JAR separate is better as it is easy to upgrade only the specific JARs to its new versions without touching any other configuration. As of your issue of having to copy each file to same location as of your JAR, you can always use Java CLASSPATHand include any JAR to your application's class path.

将 JAR 分开会更好,因为很容易将特定的 JAR 升级到新版本而不涉及任何其他配置。对于必须将每个文件复制到与 JAR 相同的位置的问题,您始终可以使用 JavaCLASSPATH并将任何 JAR 包含到应用程序的类路径中。

回答by RonU

A JAR is not itself capable of nesting other JARs, as you discovered.

正如您所发现的,JAR 本身不能嵌套其他 JAR。

Traditionally, one would distribute a ZIP archive or other installer that would unwind the application JAR (yours) as well as any support JARs in the appropriate location for classpath access. Frequently, then, the application was invoked through a script that invoked the primary JAR and created a classpath that listed the support JARs.

传统上,人们会分发一个 ZIP 存档或其他安装程序,这些安装程序可以在类路径访问的适当位置展开应用程序 JAR(您的)以及任何支持 JAR。通常,应用程序是通过调用主 JAR 的脚本调用的,并创建了一个列出支持 JAR 的类路径。

As other posters have noted, you have some options to create a super-JAR if that's what you want.

正如其他海报所指出的,如果您想要的话,您可以选择创建超级 JAR。

回答by Kel

You can use Maven + assembly plugin (http://maven.apache.org/plugins/maven-assembly-plugin/)

您可以使用 Maven + 程序集插件 ( http://maven.apache.org/plugins/maven-assembly-plugin/)

BTW, probably that's not the easiest way, if you did not work with maven.

顺便说一句,如果您不使用 maven,这可能不是最简单的方法。