Java ARchive (JAR) Files 101, Page 3
More About Manifest FileManifest is a simple text file that contains all the directions for how the JAR file will behave. When you used the jar command and created MyApplication.jar, a default MANIFEST.MF file was created under the META-INF folder. This is the default folder in which the MANIFEST.MF file is placed.
The MANIFEST.MF file contains the version of the manifest specification and information about the JDK in which it was created (see below). These are standard details that will be part of all manifest files.
Contents of the manifest file Manifest-Version: 1.0 Created-By: 1.5.0_19 (Sun Microsystems Inc.)
Remember the line Main-Class: com.mycomp.demo.MyApplication that you added to the manifest file earlier? You can do this by either editing the manifest file in the JAR or by using commands.
If you are not comfortable editing the manifest file yourself, the jar tool provides an option (m) to achieve this. So, simply modifying your create-JAR-file command to jar cmvf <jar-file-name> manifest-inclusion-file <input-file(s)> will result in the creation of a JAR file, in which the manifest file will be updated with the contents in manifest-inclusion-file. Remember that you need to have a new line at the end of the file, or the manifest file will not be created as expected. In this case, the command would be jar cmvf MyApplication.jar main-class-file.txt *.
Contents of the main-class-file Main-Class: com.mycomp.demo.MyApplication <This is a new line, which is necessary for the parser to parse this file.>
In addition to all this great stuff, Class-Path is one of the most widely used metadata attributes in manifest files. Like any Java class path, you can extend Class-Path to be part of a JAR file too. The syntax is similar to Main-Class and, in this case, it is Class-Path: <file-name1> <file-name2>. You can place this in a file similar to your main-class-file.txt or in the same file and the manifest update command will do the job for you. By definition, class paths are essential for your application to locate different classes that it will use during its execution.
For Further Reading
About the AuthorSridhar M S is a Java developer from Bangalore, India. He holds a master's degree in Computer Science.