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

时间:2021-12-30 20:44:12

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的用途。部署一个JAR文件不是更优雅吗?

6 个解决方案

#1


6  

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. 右键单击Package Explorer中的项目名称并选择Export。
  3. In Export screen, select Java -> Runnable JAR file and Next.
  4. 在导出屏幕中,选择Java ->可运行JAR文件和Next。
  5. Fill in the Runnable JAR File Spec screen and Finish.
  6. 填充可运行的JAR文件规范屏幕并完成。

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

您可以选择将依赖项jar打包为单独的jar文件,还是将它们提取到生成的jar中。

#2


4  

You could use something like One-JAR to 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嵌套成为可能)。

#3


2  

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目录。

#4


2  

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 CLASSPATH and include any JAR to your application's class path.

保持JAR独立更好,因为很容易只将特定的JAR升级到新版本,而不涉及任何其他配置。当您不得不将每个文件复制到与JAR相同的位置时,您可以始终使用Java类路径并将任何JAR包含到应用程序的类路径中。

#5


0  

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,你有一些选择。

#6


0  

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

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

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

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

#1


6  

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. 右键单击Package Explorer中的项目名称并选择Export。
  3. In Export screen, select Java -> Runnable JAR file and Next.
  4. 在导出屏幕中,选择Java ->可运行JAR文件和Next。
  5. Fill in the Runnable JAR File Spec screen and Finish.
  6. 填充可运行的JAR文件规范屏幕并完成。

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

您可以选择将依赖项jar打包为单独的jar文件,还是将它们提取到生成的jar中。

#2


4  

You could use something like One-JAR to 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嵌套成为可能)。

#3


2  

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目录。

#4


2  

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 CLASSPATH and include any JAR to your application's class path.

保持JAR独立更好,因为很容易只将特定的JAR升级到新版本,而不涉及任何其他配置。当您不得不将每个文件复制到与JAR相同的位置时,您可以始终使用Java类路径并将任何JAR包含到应用程序的类路径中。

#5


0  

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,你有一些选择。

#6


0  

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

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

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

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