Thursday, April 12, 2007

Handling jar files

Following commands needs to be executed for various purposes in managing Jar files.
  1. jar cf jar-file inputfiles: It is used to create new JAR.
  2. jar tf jar-file: It is used to view all the files of the JAR.
  3. jar xf jar-file: It is used to extract the JAR.
  4. java -jar app.jar: It is used to run a JAR if the manifest file is present in the JAR. This manifest file contains the information of the file having main method.
Code to find the contents in the jar file:
    import java.io.IOException;
    import java.util.Enumeration;
    import java.util.jar.JarFile;
    import java.util.zip.ZipEntry;

    public class ExtractJar {
        public static void main(String args[]){
       JarFile jarfile = null;
       try {
           jarfile = new JarFile("test1.jar");
       } catch (IOException e) {
          e.printStackTrace();
       }
       Enumeration entries = jarfile.entries();
       while (entries.hasMoreElements()){
          ZipEntry entry = (ZipEntry)entries.nextElement();
          System.out.println(entry.getName());
          }
       }
      }

No comments: