import java.io.*; import java.util.*; import java.util.jar.*; public class JarCreate { /** * main() */ public static void main(String[] args) throws IOException { // Create the jar file, using the first command line argument as the // file name (clobbering the file if it exists). String jarName = args[0]; JarOutputStream jar = new JarOutputStream(new FileOutputStream(jarName), new Manifest()); System.out.println(jarName + " created."); try { // Allocate a buffer for reading the input files. byte[] buffer = new byte[1024]; int bytesRead; // Loop through the file names provided on the command-line. for (int i = 1; i < args.length; i ++) { // Get the file name. String fileName = args[i]; try { // Open the file. FileInputStream file = new FileInputStream(fileName); try { // Create a jar entry and add it to the jar. JarEntry entry = new JarEntry(fileName); jar.putNextEntry(entry); // Read the file the file and write it to the jar. while ((bytesRead = file.read(buffer)) != -1) { jar.write(buffer, 0, bytesRead); } System.out.println(entry.getName() + " added."); } catch (Exception ex) { System.out.println(ex); } finally { file.close(); } } catch (IOException ex) { System.out.println(ex); } } } finally { jar.close(); System.out.println(jarName + " closed."); } } }