Java Reference
In-Depth Information
/* Add main attributes
1. Manifest Version
2. Main-Class
3. Sealed
*/
Attributes mainAttribs = manifest.getMainAttributes();
mainAttribs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
mainAttribs.put(Attributes.Name.MAIN_CLASS, "com.jdojo.intro.Welcome");
mainAttribs.put(Attributes.Name.SEALED, "true");
Adding an individual entry to the manifest file is a little more complex than adding the main entry. Suppose you
want to add the following individual entry to a manifest file:
Name: "com/jdojo/archives/"
Sealed: false
You need to perform the following steps.
1.
Get the
Map
object that stores the individual entries for a manifest.
2.
Create an
Attributes
object.
3.
Add the name-value pair to the
Attributes
object. You can add as many name-value pairs
as you want.
4.
Add the
Attributes
object to the attribute
Map
using the name of the individual section as
the key.
The following snippet of code shows you how to add an individual entry to a
Manifest
object:
// Get the Attribute map for the Manifest
Map<String,Attributes> attribsMap = manifest.getEntries();
// Create an Attributes object
Attributes attribs = new Attributes();
// Create an Attributes.Name object for the "Sealed" attribute
Attributes.Name name = new Attributes.Name("Sealed");
// Add the "name: value" pair (Sealed: false) to the attributes objects
attribs.put(name, "false");
// Add the Sealed: false attibute to the attributes map
attribsMap.put("com/jdojo/archives/", attribs);
If you want to add a manifest file to a JAR file, you can specify it in one of the constructors of the
JarOutputStream
class. For example, the following snippet of code creates a jar output stream to create a
test.jar
file with a
Manifest
object:
// Create a Manifest object
Manifest manifest = new Manifest();