简体   繁体   English

7zip 命令行 - 创建可执行的 jars

[英]7zip command line - Create executable jars

I have an executable .jar -File which works, but I want to add some files to the .jar-File with another program.我有一个可以运行的可执行文件.jar -File,但我想用另一个程序将一些文件添加到 .jar-File 中。 My idea was to use the 7zip command line extension, but when I try to add something to the .jar -File.我的想法是使用 7zip 命令行扩展,但是当我尝试向.jar -File 添加一些东西时。 I use this command:我使用这个命令:

7za.exe a -t7z C:\\myfiles\\thejar.jar C:\\filestoadd\\* -r

Everytime I try to do this the CMD throws me an error and says:每次我尝试这样做时,CMD 都会抛出一个错误并说:

Error: C:\\myfiles\\thejar.jar is not supported archive

Well, ok.好吧,好吧。 Then my Idea was to unpack the file thejar.jar , add the files to the directory where the files from thejar.jar were extracted and create a .zip with the extension .jar .然后我的想法是解压文件thejar.jar ,将文件添加到提取thejar.jar文件的目录中,并创建一个扩展名为.jar.zip When I did this, the file thejar.jar was about 1MB smaller than before with adding files to it.当我这样做时,文件thejar.jar比以前小了大约 1MB 其中添加了文件。 I tried different compression-methods but it was always smaller.我尝试了不同的压缩方法,但它总是更小。 When I tried to execute the .jar , an error-message popped up and said Invalid or corrupt jarfile .当我尝试执行.jar ,会弹出一条错误消息并说Invalid or corrupt jarfile I already googled my problem, but I haven't got an answer for now... Can you help me?我已经用谷歌搜索了我的问题,但我现在还没有得到答案......你能帮我吗?

The simple / recommended solution is to use the jar command that is included in every Java JDK to add the extra files to the JAR. 一种简单/推荐的解决方案是使用每个Java JDK中包含的jar命令,将多余的文件添加到JAR中。

It is also possible to create a JAR file using JarOutputStream : 也可以使用JarOutputStream创建一个JAR文件:

The trouble with using 7zip, or any other "standard" zip utility is that you might accidentally use some modern zipfile feature that the Java utilities don't understand. 使用7zip或任何其他“标准” zip实用程序的麻烦在于,您可能会意外地使用Java实用程序无法理解的某些现代zipfile功能。


You also asked (in comments) if you are permitted to copy the jar.exe and jli.dll from an Oracle JDK / JRE into your own project. 您还询问(在注释中)是否允许将Oracle JDK / JRE中的jar.exejli.dll复制到您自己的项目中。

  • Read the license! 阅读许可证! We are not lawyers here and we can't give you proper legal advice. 我们不是律师,我们不能给您适当的法律建议。

  • My understanding is that that would be a violation of the Oracle license and copyright. 我的理解是,这将违反Oracle许可和版权。 Embedding a full JRE is permitted, but cherry-picking is not permitted. 允许嵌入完整的JRE,但不允许选择樱桃。

  • Consider using OpenJDK instead. 考虑改用OpenJDK。 The licensing for OpenJDK is GPL based and there are no copyright-based restrictions on redistributing a vanilla distribution. OpenJDK的许可基于GPL,并且对重新分发原始发行版没有基于版权的限制。 Not even modifying and redistributing it ... so long as you make available source code and build instructions for any changes you make to it 1 . 只要您提供可用的源代码并为您对其进行的任何更改构建说明,它们甚至都不会进行修改和重新分发... 1


Finally, note that this is moot in Java 11 onwards. 最后,请注意,从Java 11开始,这是没有意义的。 Oracle now only distributes full JDK's. Oracle现在仅分发完整的JDK。 So that means that if your customer installs "Java" from Oracle, the jar command will be included. 因此,这意味着如果您的客户从Oracle安装“ Java”,则将包含jar命令。


1 - You only have to worry about trademark. 1-您只需要担心商标。 Basically, the trademark license says you are not permitted to call a product "Java", etc if it deviates from the "standard" in non-permitted ways. 基本上,商标许可说如果您以非许可的方式偏离“标准”,则不允许将其称为“ Java”产品。 Lookup the details for yourself. 查找自己的详细信息。

-t7z will create a 7z archive, not a zip. -t7z将创建7z存档,而不是zip。 You need -tzip instead. 您需要-tzip代替。

I know this is an old post but if anyone is searching for info the following works great and makes a jar that will execute correctly, I ran across this post myself looking for info and in the end came up with the following.我知道这是一篇旧帖子,但如果有人正在搜索信息,以下内容很好用,并且制作了一个可以正确执行的 jar,我自己浏览了这篇文章,寻找信息,最后想出了以下内容。

For me this method was the easiest way to do what I needed, and just easier and faster then using jar, plus works great as batch file对我来说,这种方法是做我需要的最简单的方法,而且比使用 jar 更容易、更快,而且作为批处理文件效果很好

C:\\Progra~1\\7-Zip\\7z.exe a -tzip -mx9 -spf -r Filename.jar * C:\\Progra~1\\7-Zip\\7z.exe a -tzip -mx9 -spf -r Filename.jar *

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM