Java : How to create a Jar with single or multiple files

How to create a jar file.

We can make a jar file with a single class using the following commands

Here can be replaced by the name of the jar file you want.

1. Create a jar file with single file

jar --create --file=<FileName> <file-1>

2. Create a jar file with multiple files

jar --create --file=<FileName> <file-1> <file-2>

3. Create a jar file with all files in a directory

jar --create --file="<FileName> -C /path/to/dir/ ."

Here -C specifies the directory and . specifies that it need to include all the files in te directory.

Some of the shorthands for the above commands

Option Shorthand
--create -c
--file -f
--module-path -p
--verbose -v
--list -t
--extract -x


How to extract a jar file

jar -xf <FileName>

How to list the contents of a jar file

jar -tf <FileName>

No comments :

Post a Comment