7zip command line - Create executable jars

13,813

Solution 1

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 it1.


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.

Solution 2

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

Share:
13,813

Related videos on Youtube

DigitalClark
Author by

DigitalClark

Updated on September 15, 2022

Comments

  • DigitalClark
    DigitalClark over 1 year

    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?

  • DigitalClark
    DigitalClark over 11 years
    Yea, I already thought of that, but that means the user of my program needs to install JDK. I could copy jar.exe, but I don't know if this violates the copyright of Oracle.
  • DigitalClark
    DigitalClark over 11 years
    Oh yea, well, jar.exe works now, but am I allowed to copy jar.exe and jli.dll for my project? I don't want to violate any copyrights, so... (This project is non-profit for me)
  • Ian Roberts
    Ian Roberts over 11 years
    You talk about starting with "an executable jar file" so we can safely assume your users have a JRE even if not JDK. In that case an alternative might be to write some code that uses Ant to build the JAR, or code it yourself using ZipOutputStream (which is what the jar command uses internally).
  • DigitalClark
    DigitalClark over 11 years
    Awesome. Thanks. I will look what I can do. But first, I have another question. Can I copy jar.exe and jli.dll (requiered for jar.exe) and add it to my own project? I am asking this because this is made by Oracle and I don't want to violate their copyright. That would mean I would reupload it and republish it (This is non-profit for me).