3

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?

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
DigitalClark
  • 57
  • 2
  • 3
  • 5
  • http://stackoverflow.com/questions/2223434/appending-files-to-a-zip-file-with-java may help you to do what you require in pure Java. – Ian Roberts Oct 14 '12 at 19:56

3 Answers3

5

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.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • 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 Oct 14 '12 at 12:44
  • 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) – DigitalClark Oct 14 '12 at 13:04
  • 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). – Ian Roberts Oct 14 '12 at 13:48
  • 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). – DigitalClark Oct 14 '12 at 14:54
2

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

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
1

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 *

Gene
  • 11
  • 2