What is JAR?
- JAR stands for Java Archival
What is Archival ?
- Grouping of files together in a single file is called archive
How to create JAR ?
- Using JAR command ( C for create JAR)
jar c <class-file-name1>
- But using above command the JAR contents is writtren into Standard Output, to write the content into a file (f for file) f attribute has to be used
jar cf <jar-file> <class-file-name1>
- You can add v attribute to show verbose output
jar cfv <jar-file> <class-file-name1>
- You can also add a directory instead of file as JAR attribute
jar cfv <jar-file> <dir>
- As you can see in above example bin directory got included in the jar and if you do not want to the parent bin folder to be see inside jar to you direct JAR command to cd to a directory and then pull files. (-C command achieves this )
jar cfv <jar-file> -C bin/ .
- You can add m attribute (m for creating manifest file) for creating the manifest file inside JAR.(Purpose of manifest file is to create a runnable jar)
View JAR contents using below command
jar tf <jar-file-name>
- Extract a file from JAR using below command
jar xf jar-file <file-to-be-extracted-1>
- Update the contents of JAR
jar uf jar-file input-file(s)
- Executing a executable JAR (Executable i.e entry in Manifest file)
java -jar JarExample.jar "arg1"
- Executing a non executable JAR
java -cp JarExample.jar com.demo.FileDemo "args1"