I got this jar creator java code in the internet. It creates a nice jar file out from your java codes but there is a a problem. If you you run the jar file, it creates an error because the manifest is empty. I think there is a missing line because the manifest file should look like this:

Manifest-Version: 1.0
Created-By: 1.7.0-ea (Sun Microsystems Inc.)
Main-Class: ProgressBarStep

is there any code that I need to add in order the manifest file will not be empty?

import java.io.*;
import java.util.jar.*;

public class CreateJar {
  public static int buffer = 10240;
  protected void createJarArchive(File jarFile, File[] listFiles) {
    try {
      byte b[] = new byte[buffer];

      Manifest manifest = new Manifest();

      FileOutputStream fout = new FileOutputStream(jarFile);

      JarOutputStream out = new JarOutputStream(fout, manifest);

      for (int i = 0; i < listFiles.length; i++) {
        if (listFiles[i] == null || !listFiles[i].exists()|| listFiles[i].isDirectory())
      System.out.println();
        JarEntry addFiles = new JarEntry(listFiles[i].getName());
        addFiles.setTime(listFiles[i].lastModified());
        out.putNextEntry(addFiles);

        FileInputStream fin = new FileInputStream(listFiles[i]);
        while (true) {
          int len = fin.read(b, 0, b.length);
          if (len <= 0)
            break;
          out.write(b, 0, len);
        }
        fin.close();
      }
      out.close();
      fout.close();
      System.out.println("Jar File is created successfully.");
    } catch (Exception ex) {}
  }
  public static void main(String[]args){
    CreateJar jar=new CreateJar();
    File folder = new File("D://Programs//Java//progressbar//demo1");
      File[] files = folder.listFiles();
    File file=new File("D://Programs//Java//progressbar//demo1//ProgressBarStep.jar");
    jar.createJarArchive(file, files);
  }
}

the code above creates an empty manifest file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.