150 likes | 542 Views
Understanding and Creating Jar files in Java. Jar Files. Jar Files. The Java TM Archive (JAR) file format enables you to bundle multiple files into a single archive file. Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications
E N D
Jar Files • The JavaTM Archive (JAR) file format enables you to bundle multiple files into a single archive file. • Typically a JAR file will contain the class files and auxiliary resources associated with applets and applications • Jar files encompass a Zip format • Files are compressed to save space. • Can be digitally signed. • Programs can be run from Jar files without being un-archived.
Jar Files • Use the Jar tool to create Jar files • Syntax • Jar [options][manifest] destination input-file • c:\ Jar cf jarfile.jar *.class • This would “package” all the class files in the folder. • Jar cf jarfile.jar *.* • This would “package” all the files in the folder.
Jar Files • Options • c creates a new jar file • f the argument following this option specifies a Jar file to work with. • t lists the table of contents • x [file] extracts all the files • If [file] is named, it will extract only that file • m includes manifest information • M specifies that a manifest NOT be created • u Updates an existing Jar file by adding files or changing the manifest.
Jar Manifest Files • Manifest files are text files. • Include • Manifest-version: 1.1 • Main-Class: {name of class file with main function} • (remember to have a blank line here) • File is named myManifest.mf • NOTE file must end with a blank line.
Jar File Usage • Getting the contents of a jar • jar tf myfile.jar • Extracting files • jar xf myfile.jar // extracts all the files • Jar xf myfile.jar my.class // extracts the my.class • Updating a jar • jar uf myfile.jar myclass.class • Building a jar • Jar cmf mymanifest.mf myfile.jar *.class • This would only include the class files • Jar cmf mymanifest.mf myfile.jar *.class *.java • This would include the java files and class files