How can I add files to a Jar file?

jar -uf my.jar file1 file2...
jar -uf my.jar dir/

or mixed

jar -uf my.jar file dir/

A JAR file is a ZIP file, remember.

Just use some ZIP library.


Just to add to the existing answers, there is at least one special case: so-called executable JAR files. If you add another JAR file as a dependency -- whether you use jar or zip -- it will complain that the embedded file is compressed:

Caused by: java.lang.IllegalStateException: Unable to open nested entry 'BOOT-INF/lib/file.jar'. It has been compressed and nested jar files must be stored without compression. Please check the mechanism used to create your executable jar file

The solution to this is to use the 0 option to jar:

jar uvf0 myfile.jar BOOT-INF/lib/file.jar

You don't need this for normal class files.


jar -u file.jar file1 file2 file3 ...

Tags:

Java

Jar