Homepage of Ingo Klöckl
 Home
 Programmieren
 - Toolbox
 - Java
 - PostScript
 - Perl
 - Assembler
 - Links
 - Download
Ingo Klöckl
i.kloeckl@2k-software.de

Manipulating Java archives

Here I want you to present two short code snippets, showing how you can work with Java archives (jar, war and ear files). For these examples, the XML manipulation library JDom is used.

TODO

Reading Java archives

blabla

(the code can be downloaded here):
package de.kksoftware.demo.xml;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;

public class ReadJarFile {

  public static void main(String[] args) throws Exception {
    // open an EJB jar file
    JarFile oJar = new JarFile(new File("GeneralKey.jar"));
    // print out all entries of the archive
    Enumeration oEntries = oJar.entries();
    while (oEntries.hasMoreElements()) {
      JarEntry oEntry = (JarEntry)oEntries.nextElement();
      System.out.println(".. "+oEntry.getName());
    }

    // look for the ejb-jar.xml entry    
    JarEntry oEJBJarEntry = oJar.getJarEntry("META-INF/ejb-jar.xml");

    // read the deployment descriptor into JDom
    InputStream oInStream = oJar.getInputStream(oEJBJarEntry);
    SAXBuilder oBuilder = new SAXBuilder();
    Document oEjbDoc = oBuilder.build(oInStream);
    oInStream.close();
    Element oRoot = oEjbDoc.getRootElement();
    System.out.println("The root element of ejb-jar.xml is "+oRoot.getName());
    // do other XML stuff with the deployment descriptor
  }
}

Writing Java archives

blabla

(the code can be downloaded here):
package de.kksoftware.demo.xml;

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

import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.XMLOutputter;

public class WriteJarFile {

  public static void main(String[] args) throws Exception {
    // open an EJB jar file
    JarFile oJar = new JarFile(new File("GeneralKey.jar"));

    // create a stream to the new archive
    OutputStream oOut = new FileOutputStream(new File("MyKey.jar"));
    JarOutputStream oJarOut = new JarOutputStream(oOut);
    
    // copy all entries of the archive except the deployment descriptor
    int iSize = 0;
    byte[] oBuffer = new byte[1024];
    Enumeration oEntries = oJar.entries();
    while (oEntries.hasMoreElements()) {
      JarEntry oEntry = (JarEntry)oEntries.nextElement();
      if (!oEntry.getName().equals("META-INF/ejb-jar.xml")){
        System.out.println ("copying jar entry "+oEntry.getName()+" ...");
        oJarOut.putNextEntry(oEntry);
        InputStream oJarInStream = oJar.getInputStream(oEntry);
        while ((iSize = oJarInStream.read(oBuffer))!=-1){
          oJarOut.write(oBuffer, 0, iSize);
        }
        oJarInStream.close();
      }
    }
    // create my own descriptor
    Element oRoot = new Element("myejb-jar");
    Document oDoc = new Document(oRoot);
    Element oNode = new Element("mynode");
    oRoot.addContent(oNode);
    // write it instead of the original one
    System.out.println ("writing myejb-jar.xml");
    oJarOut.putNextEntry(new JarEntry("META-INF/myejb-jar.xml"));
    XMLOutputter oXMLOut = new XMLOutputter("  ", true);
    oXMLOut.output(oDoc, oJarOut);

    // all done
    oJarOut.close();
    oOut.close();
  }
}

back to toolbox page