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())
        JarEntry addFiles = new JarEntry(listFiles[i].getName());

        FileInputStream fin = new FileInputStream(listFiles[i]);
        while (true) {
          int len = fin.read(b, 0, b.length);
          if (len <= 0)
          out.write(b, 0, len);
      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);

Edited 4 Years Ago by peter_budo: Please do not use CODE SNIPPET for posting questions. Keep it as DISCUSSION THREAD instead

This article has been dead for over six months. Start a new discussion instead.