如何重新打包 jar 文件及其所有依赖项?
我正在开发一个应该独立运行的应用程序。但是,这个项目涉及一个包含很多依赖项的 .jar 文件,如果我只是将这个 .jar 文件与应用程序一起分发,它将无法工作。
我想知道是否有任何方法可以解压文件,添加依赖项并再次重新打包?我希望有一些自动机制可以实现这一点,因为手动过程可能需要几个小时,并且可能还有其他引用的 jar 文件。
PS 我正在使用 Eclipse,但由于我要使用 Web Start 部署此项目,因此使用内置导出工具导出项目可能不是一个好主意,因为我的尝试都以 ClassNotFoundException,所以我怀疑我可能必须将项目打包到几个罐子中。
谢谢!
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.
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.
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
由于文件夹结构的原因,重新打包已解压的 JAR 有点令人沮丧
解压时:
您会得到一个
JAR_NAME/
文件夹要重新打包 JAR:
删除旧 jar
rm JAR_NAME.jar
进入文件夹
cd JAR_NAME
打包引用父文件夹的 jar
jar cf ../JAR_NAME.jar *
,您将结束与父文件夹中的
JAR_NAME.jar
一起,原始文件是从父文件夹中解压出来的,如果您打包了文件夹本身,则不会获得第一个文件夹级别。Repacking an unpacked JAR is a little frustrating because of the folder structure
When unpacking with:
you get a
JAR_NAME/
folderTo repack the 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 *
and you will end up with the
JAR_NAME.jar
in the parent folder, where the original was unpacked from, without the first folder level you would get if you had packed the folder itself.对于 Spring Boot 2.1.9.RELEASE,我设法解压并重新打包 fat jar,如下所示:
移至包含 jar 文件的文件夹中。
提取 jar 文件
删除旧的 jar 文件
在父目录中创建新的 jar 文件
m ... 使用特定的清单文件
0 ... 停用压缩,这可以避免依赖项(jar 文件)被压缩
文档也可以在此处找到: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
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.
Extract jar file
Remove old jar file
Create new jar file in parent dir
m ... use a specific manifest-file
0 ... deactivates compression, which circumvents that the dependencies(jar-files) are compressed
Documentation can also be found here: https://docs.oracle.com/javase/tutorial/deployment/jar/build.html
是的,解包和打包有时会让您恼火,尤其是在 Ubuntu 等 Linux 环境中,
在解包之前,请确保您的 Java_Home 和路径已导出到以下文件夹:
在您的主目录中,您很可能有一个名为 .bashrc 的文件。转到文件末尾并添加以下内容
这将设置您的 JAVA_HOME 环境变量 - 通常由基于 Java 的应用程序使用。然后它将该目录添加到您的 PATH 变量中。操作系统将 PATH 理解为可执行文件的父目录列表。
现在解压缩
jar xvf JAR_NAME.jar
打包回来
jar cf JAR_NAME.jar *
Yes unpacking and packing will irritate you sometimes especially in Linux environments like Ubuntu,
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
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.
Now unpack
jar xvf JAR_NAME.jar
Packing back
jar cf JAR_NAME.jar *
查看jar jar。听起来它会做你需要的事情。
Have a look at jar jar. It sounds like it will do what you need.
您是否尝试过在 Eclipse 中“导出可运行的 jar”?它应该适合你。
看看这张照片:
Have you tried "Export runnable jar" in eclipse? It should works for you.
Take a look at this picture: