简体   繁体   中英

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. My idea was to use the 7zip command line extension, but when I try to add something to the .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:

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 . When I did this, the file thejar.jar was about 1MB smaller than before with adding files to it. 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 . 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.

It is also possible to create a JAR file using JarOutputStream :

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.


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.

  • 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. Embedding a full JRE is permitted, but cherry-picking is not permitted.

  • Consider using OpenJDK instead. The licensing for OpenJDK is GPL based and there are no copyright-based restrictions on redistributing a vanilla distribution. 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 .


Finally, note that this is moot in Java 11 onwards. Oracle now only distributes full JDK's. So that means that if your customer installs "Java" from Oracle, the jar command will be included.


1 - You only have to worry about trademark. 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. Lookup the details for yourself.

-t7z will create a 7z archive, not a zip. You need -tzip instead.

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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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