Java 将多个罐子合并成一个罐子

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

Merging Multiple Jars in to a Single Jar

javajar

提问by Hamza Yerlikaya

my application needs multiple jars to work. Since it is a desktop application i can not hold the user responsible of installing. So in my build script i unzip the jars content in to my build directory delete manifest files, compile my software and jar it again. Everything works as it should my question is are there any long term side effects to this process?

我的应用程序需要多个 jars 才能工作。由于它是桌面应用程序,我无法让用户负责安装。因此,在我的构建脚本中,我将 jars 内容解压缩到我的构建目录中,删除清单文件,编译我的软件并再次对其进行 jar 包。一切正常,我的问题是这个过程是否有任何长期的副作用?

采纳答案by Aaron Digulla

In the past, there were JARs with weird content (like the DB2 driver which contains com.ibmand com.IBM; after decompressing in a Windows filesystem, those two packages would be merged).

过去,有一些内容很奇怪的 JAR(如 DB2 驱动程序,其中包含com.ibmcom.IBM; 在 Windows 文件系统中解压缩后,这两个包会合并)。

The only issue you need to be aware of are signed jars and other files in META-INF which might have the same name in multiple source JARs.

您需要注意的唯一问题是 META-INF 中的签名 jar 和其他文件,它们在多个源 JAR 中可能具有相同的名称。

A simple solution for all these issues is to use One-JAR. It allows to wrap several JARs into one without unpacking them, first. And read this answer: Easiest way to merge a release into one JAR file

所有这些问题的一个简单解决方案是使用One-JAR。首先,它允许将多个 JAR 打包成一个,而无需解压它们。并阅读此答案:将发布合并到一个 JAR 文件的最简单方法

回答by asalamon74

If some of the jars are signed you lose the signature by unpacking/repacking it.

如果某些罐子有签名,您会因拆包/重新包装而丢失签名。

回答by Glen

Well you're throwing away the MANIFEST of your third party jars, so that could cause you problems. For example you could be causing security issues by throwing away the "Sealed" attribute.

好吧,您正在丢弃第三方罐子的清单,因此这可能会给您带来问题。例如,您可能会因为丢弃“密封”属性而导致安全问题。

Why not just create a simple installer and a script to launch your application which sets the CLASSPATH correctly?

为什么不创建一个简单的安装程序和脚本来启动正确设置 CLASSPATH 的应用程序?

回答by James McMahon

If you want a no fuss way for the end user to kick off a program with multiple jar dependencies you may want to look at Launch4jor Jsmooth(I prefer Launch4j). Both are programs that create executables that wrap jar(s) and the JRE together so that to the end user it appears no different then any other executable.

如果您希望最终用户以一种轻松的方式启动具有多个 jar 依赖项的程序,您可能需要查看Launch4jJsmooth(我更喜欢 Launch4j)。两者都是创建将 jar(s) 和 JRE 包装在一起的可执行文件的程序,以便对最终用户来说,它看起来与任何其他可执行文件没有什么不同。

回答by Lawrence Dol

Another great option is ProGuard, which can alsoshrink and/or obfuscate the code too.

另一个不错的选择是ProGuard,它也可以缩小和/或混淆代码。

回答by swamy

If your primary target platform is Windows desktop, then you could also consider generating an Windows native exe from the jars of your application

如果您的主要目标平台是 Windows 桌面,那么您还可以考虑从应用程序的 jars 生成 Windows 本机 exe

回答by simontuffs

One-JAR will do the job, and has a new release (0.97) which supports frameworks like Spring and Guice, which users are now packing into One-JAR archives. http://one-jar.sourceforge.net

One-JAR 将完成这项工作,并有一个新版本 (0.97),它支持 Spring 和 Guice 等框架,用户现在将它们打包到 One-JAR 档案中。 http://one-jar.sourceforge.net

Ference Hechler also did some great work inside Eclipse with the Eclipse export wizard: we worked together on FatJar/One-JAR from which the Eclipse work grew, and I can recommend that as an approach, though I don't know how well it handles the frameworks.

Ference Hechler 还在 Eclipse 中使用 Eclipse 导出向导做了一些很棒的工作:我们在 FatJar/One-JAR 上一起工作,Eclipse 工作从中发展,我可以推荐它作为一种方法,尽管我不知道它处理得如何框架。

回答by Ophir

A simpler solution (IMO) is using Maven's assembly plugin, which is also described in one of the answers to another question which was linked to in a previous Q&A. This is provided you are using Maven (which is a recommended tool by its own right) as a build tool.

一个更简单的解决方案 (IMO) 是使用Maven 的程序集插件,这也在之前的问答中链接到的另一个问题的答案之一中进行了描述。前提是您使用 Maven(它本身就是推荐的工具)作为构建工具。