"jar -cvf" to Create a JAR with Files

Q

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

✍: FYIcenter.com

A

To create a JAR file with a list of files, you can use the "jar -cvf jarname filenames" command.

For exame, the sesson below creates a new JAR file with all class files in the current folder,

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

\fyicenter>copy javax\jms\*.class .
javax\jms\BytesMessage.class
javax\jms\Connection.class
javax\jms\ConnectionConsumer.class
javax\jms\ConnectionFactory.class
...

\fyicenter>"%java_home%\bin\jar" -cvf myClass.jar *.class
added manifest
adding: BytesMessage.class(in = 1202) (out= 494)(deflated 58%)
adding: Connection.class(in = 945) (out= 373)(deflated 60%)
adding: ConnectionConsumer.class(in = 275) (out= 189)(deflated 31%)
adding: ConnectionFactory.class(in = 311) (out= 179)(deflated 42%)
...

\fyicenter>del *.class

\fyicenter>"%java_home%\bin\jar" -tf myClass.jar
META-INF/
META-INF/MANIFEST.MF
BytesMessage.class
Connection.class
ConnectionConsumer.class
ConnectionFactory.class
ConnectionMetaData.class
DeliveryMode.class
...

Note that how the "jar" command automatically added a default a MANIFEST.MF file into the JAR.

By the way, the JVM will not like this JAR file, because classes must be added to the JAR with their package names maintained as folder names. See the next tutorial on how to fix this.

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

2015-11-07, 2525👍, 0💬