java 我怎样才能重新打包一个 jar 文件及其所有依赖项?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6629361/
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 16:42:39  来源:igfitidea点击:

How could I repack a jar file with all of its dependencies?

javaimportjardependenciesexport

提问by zw324

I am developing an application which is supposed to run standalone. However, this project involves a .jar file which contains a lot of dependencies, and if I simply distribute this .jar file with the application, it won't work.

我正在开发一个应该独立运行的应用程序。但是,这个项目涉及一个.jar 文件,其中包含很多依赖项,如果我简单地将这个.jar 文件与应用程序一起分发,它将无法工作。

I wonder if there is any way in which I could unpack the file, add the dependencies and repack it again? I hope there are some automatic mechanism for this, since the manual process could take hours, and there might be other referenced jar files.

我想知道是否有任何方法可以解压缩文件,添加依赖项并重新打包?我希望有一些自动机制,因为手动过程可能需要几个小时,并且可能还有其他引用的 jar 文件。

P.S. I am using Eclipse, but since I am going to deploy this project with Web Start, exporting the project with the build-in export tool might not be a good idea since my attempts all ended up with ClassNotFoundException, so I suspect I might have to pack the project into several jars.

PS 我正在使用 Eclipse,但由于我将使用 Web Start 部署此项目,因此使用内置导出工具导出项目可能不是一个好主意,因为我的尝试都以ClassNotFoundException告终,所以我怀疑我可能有将项目打包成几个罐子。

Thanks!

谢谢!

采纳答案by thunderflower

Have a look at jar jar. It sounds like it will do what you need.

看看jar jar。听起来它会做你需要的。

回答by MrE

Repacking an unpacked JAR is a little frustrating because of the folder structure

由于文件夹结构的原因,重新打包解压的 JAR 有点令人沮丧

When unpacking with:

打开包装时:

jar xvf JAR_NAME.jar

you get a JAR_NAME/folder

你得到一个JAR_NAME/文件夹

To repack the JAR:

重新打包 JAR:

  • remove old jar

    rm JAR_NAME.jar

  • get inside the folder

    cd JAR_NAME

  • pack the jar referencing the parent folder

    jar cf ../JAR_NAME.jar *

  • 取出旧罐子

    rm JAR_NAME.jar

  • 进入文件夹

    cd JAR_NAME

  • 打包引用父文件夹的 jar

    jar cf ../JAR_NAME.jar *

and you will end up with the JAR_NAME.jarin the parent folder, where the original was unpacked from, without the first folder level you would get if you had packed the folder itself.

并且您最终将JAR_NAME.jar在父文件夹中,原始文件夹从中解压缩,如果您打包文件夹本身,则没有第一个文件夹级别。

回答by HariKishore

Yes unpacking and packing will irritate you sometimes especially in Linux environments like Ubuntu,

是的,有时拆包和打包会让你感到恼火,尤其是在像 Ubuntu 这样的 Linux 环境中,

Before unpacking make sure your Java_Home and path is exported to the folder as below : In your home directory, you will most likely have a file called .bashrc. Go to the end of the file and add the following

在解包之前,请确保您的 Java_Home 和路径已导出到以下文件夹: 在您的主目录中,您很可能有一个名为 .bashrc 的文件。转到文件末尾并添加以下内容

export JAVA_HOME=<path to your JDK install e.g. /opt/jdk>
export PATH=$JAVA_HOME/bin:$PATH

This sets your JAVA_HOME environment variable - commonly used by Java-based applications. It then prepends that directory to your PATH variable. The operating system understands PATH as a list of the parent directories of executable files.

这将设置您的 JAVA_HOME 环境变量 - 通常由基于 Java 的应用程序使用。然后将该目录添加到您的 PATH 变量中。操作系统将 PATH 理解为可执行文件的父目录列表。

Now unpack jar xvf JAR_NAME.jar Packing backjar cf JAR_NAME.jar *

现在拆箱jar xvf JAR_NAME.jar 包装回来jar cf JAR_NAME.jar *

回答by anstue

For Spring Boot 2.1.9.RELEASE I managed to unpack and re-pack the fat jar like this: Move into the folder with the jar file.

对于 Spring Boot 2.1.9.RELEASE,我设法像这样解压并重新打包 fat jar:移动到包含 jar 文件的文件夹中。

Extract jar file

解压jar文件

jar xvf app.jar

Remove old jar file

删除旧的 jar 文件

rm app.jar

Create new jar file in parent dir

在父目录中创建新的 jar 文件

jar cmf0 META-INF/MANIFEST.MF ../app.jar  *

m ... use a specific manifest-file

m ... 使用特定的清单文件

0 ... deactivates compression, which circumvents that the dependencies(jar-files) are compressed

0 ... 停用压缩,从而避免压缩依赖项(jar 文件)

Documentation can also be found here: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html

文档也可以在这里找到:https: //docs.oracle.com/javase/tutorial/deployment/jar/build.html

回答by KrzyH

Have you tried "Export runnable jar" in eclipse? It should works for you.

您是否尝试过在 Eclipse 中“导出可运行的 jar”?它应该适合你。

Take a look at this picture: picture

看看这张照片: 图片