4

I am reading packaging java applications with jar tool. I noticed a manifest file is created under META-INF directory. For a simple application, it felt like, it's serves no purpose. I searched on stackoverflow to understand the usages of Manifest file. I came across UsesOfManifestFile. While reading the answer, i got confused on point 2 and 3 which are :-

  1. What are download extensions? I am not able to understand the concept in the answer.
  2. What does it mean to seal the jar? It has given an example below like this

Name: myCompany/MyPackage/ Sealed: true

What is the use of putting such information? Can someone elaborate on these points. Thanks.

Community
  • 1
  • 1
benz
  • 4,561
  • 7
  • 37
  • 68

1 Answers1

4

The manifest file is like the guidance instructions for the java program to run the jar. When the manifest is created it will hold crucial information, for example a reference to the main program. When the java program runs your jar it doesn't know where to start, so it look sin the manifest for a line telling it where the class with the main method is so it has a start point for the program. Another use is a classpath line, which will tell the program where to find any 3rd party library's in the jar are, otherwise again, the java program won't find them.

There is a range of data that can be stored in the manifest file, I would recommend checking out the Oracle information on them and seeing if that clears it up a bit more.

EDIT: From the Oracle site regarding your example:

Packages within JAR files can be optionally sealed, which means that all classes defined in that package must be archived in the same JAR file. You might want to seal a package, for example, to ensure version consistency among the classes in your software.

You seal a package in a JAR file by adding the Sealed header in the manifest, which has the general form:

Name: myCompany/myPackage/ Sealed: true The value myCompany/myPackage/ is the name of the package to seal.

Note that the package name must end with a "/".

What this appears to mean is that any and all classes that you use in your program must be within the same jar file.

EDIT 2 (For comment response)

A manifest may contain the following line:

Main-Class: com.mkyong.awt.AwtExample  

When the javaw.exe (I think) runs your runnable jar, it has no idea where to start from, all java programs run via a main method, but if you have say 50 class files in your jar, it has no idea which one has the main method to start from. It will look at the manifest file, reads your above line and thinks right, the main method is in the package com.mkyong.awt and the class is AwtExample, it then finds this class and runs the program from that main method. It's most basic function within the jar is to tell the java program running the jar where to find the stuff it needs to run your jar.

Note: that example manifest entry came from a little tutorial which shows how to create a simple runnable jar.

Good Luck!

Levenal
  • 3,796
  • 3
  • 24
  • 29
  • so it means instructions are basically written in manifest file are not only textual data. They are like special commands which are read by the jar file when we run it. Like you gave an example of main method in a class. – benz Nov 19 '13 at 08:43
  • @benz Yes, when you create the manifest you do it possibly using something like notepad, but when the jre runs your jar it will look at those lines/commands when it needs something, like information on where the main method is, or where to find a 3rd party class. I have re-edited my answer with a small example of a line explaining where the main class is – Levenal Nov 19 '13 at 08:55