Java Reference
In-Depth Information
// Create a JarOutputStream with a Manifest object
JarOutputStream jos = new JarOutputStream(new BufferedOutputStream(
new FileOutputStream("test.jar")), manifest);
Listing 9-5 contains the code to create a JAR file that includes a manifest file. The code is similar to creating a ZIP
file. The main() method contains the file names used to create the JAR file. All files are expected to be in the current
working directory.
It creates a JAR file named
jartest.jar .
It adds an
images/logo.bmp and com/jdojo/archives/Test.class files to the jartest.jar
file.
If the input files do not exist in your current working directory, you will get an error message when you run the
program. If you want to add other files to the JAR file, please change the code in the main() method accordingly.
Listing 9-5. Creating a JAR File Using the JAR API
// JARUtility.java
package com.jdojo.archives;
import java.util.jar.Manifest;
import java.util.jar.Attributes;
import java.util.Map;
import java.util.jar.JarOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.File;
import java.util.zip.Deflater;
import java.io.BufferedOutputStream;
import java.util.jar.JarEntry;
public class JARUtility {
public static void main(String[] args) throws Exception {
// Create a Manifest object
Manifest manifest = getManifest();
// Store jar entries in a String array
String jarFileName = "jartest.jar";
String[] entries = new String[2];
entries[0] = "images/logo.bmp";
entries[1] = "com/jdojo/archives/Test.class";
createJAR(jarFileName, entries, manifest);
}
public static void createJAR(String jarFileName,
String[] jarEntries,
Manifest manifest) {
 
Search WWH ::




Custom Search