"jar -cvf" to Create a JAR with a Folder

Q

What is the "jar" command to create a JAR file with files in a folder?

✍: FYIcenter.com

A

To create a JAR file with all files in a folder, you can use the "jar -cvf jarname foldername" command.

For example, the following session creates a new JAR file with all files in a sub folder,

\fyicenter>"%java_home%\bin\jar" -xvf jms.jar

\fyicenter>"%java_home%\bin\jar" -cvf myFolder.jar .\javax
added manifest
adding: javax/(in = 0) (out= 0)(stored 0%)
adding: javax/jms/(in = 0) (out= 0)(stored 0%)
adding: javax/jms/BytesMessage.class(in = 1202) (out= 494)(deflated 58%)
adding: javax/jms/Connection.class(in = 945) (out= 373)(deflated 60%)
adding: javax/jms/ConnectionConsumer.class(in = 275) (out= 189)(deflated 31%)
adding: javax/jms/ConnectionFactory.class(in = 311) (out= 179)(deflated 42%)
adding: javax/jms/ConnectionMetaData.class(in = 544) (out= 271)(deflated 50%)
adding: javax/jms/DeliveryMode.class(in = 207) (out= 160)(deflated 22%)
adding: javax/jms/Destination.class(in = 113) (out= 95)(deflated 15%)
...

This time, the new JAR, myFolder.jar, is good to be used with the JVM. The only thing we need to improve is the add the original MANIFEST.MF back to the JAR. See the next tutorial on how to fix this.

Back to FAQ for JDK JAR (Java ARchive) Tool.

2015-11-09, 6129🔥, 0💬