JavaXT

Jar Class

Used to find entries in a jar file associated with a given class or package. The original motivation behind this class was to support a requirement to extract and update config files stored in Java packages. For console apps, the config file is stored in the jar (zip) file. For web apps, chances are that the package has been un-zipped and the config file is laying around on disk. This class was designed to support both use cases.

Constructors

public Jar( Object object )
public Jar( Class Class )
public Jar( Package Package )
public Jar( java.io.File file )

Methods

getEntries( ) returns javaxt.io.Jar.Entry[]
Used to return a list of all the entries found in the jar file.
getEntry( String Entry ) returns javaxt.io.Jar.Entry
Used to retrieve a single entry from the jar file.
getEntry( Class Class ) returns javaxt.io.Jar.Entry
Used to retrieve a single entry from the jar file.
getEntry( String Package, String Entry ) returns javaxt.io.Jar.Entry
Used to retrieve a single entry from the jar file.
PackageName of the package or directory in the jar file (e.g. "javaxt.io"). Null values and zero length strings default to the the root directory.
EntryName of the class/file found in the given package (e.g. "Jar.class").
getFile( ) returns java.io.File
Returns a java.io.File representation of the jar file or directory where the jar file has been extracted.
getJars( Package Package ) returns javaxt.io.Jar[]
Returns an array of files or directories associated with a given Package. This method should be used instead of new Jar(java.lang.Package).
getJars( String packageName ) returns javaxt.io.Jar[]
getManifest( ) returns java.util.jar.Manifest
Returns the Manifest file found in the "META-INF" directory. The Manifest file contains metadata for the jar file including version numbers, vendor name, etc. You can loop through properties in the Manifest like this:
    java.io.File file = new java.io.File("/Drivers/h2/h2-1.3.162.jar");
    java.util.jar.JarFile jar = new javaxt.io.Jar(file);
    java.util.jar.Manifest manifest = jar.getManifest();

    System.out.println("\r\nMain Attributes:\r\n--------------------------");
    printAttributes(manifest.getMainAttributes());


    System.out.println("\r\nOther Attributes:\r\n--------------------------");
    java.util.Map<String, java.util.jar.Attributes> entries = manifest.getEntries();
    java.util.Iterator<String> it = entries.keySet().iterator();
    while (it.hasNext()){
        String key = it.next();
        printAttributes(entries.get(key));
        System.out.println();
    }

    jar.close();

    private static void printAttributes(java.util.jar.Attributes attributes){
        java.util.Iterator it = attributes.keySet().iterator();
        while (it.hasNext()){
            java.util.jar.Attributes.Name key = (java.util.jar.Attributes.Name) it.next();
            Object value = attributes.get(key);
            System.out.println(key + ":  " + value);
        }
    }
   
getVersion( ) returns String
Returns the version number of the jar file, if available. Two different strategies are used to find the version number. First strategy is to parse the jar file manifest and return the value of the "Implementation-Version" or "Bundle-Version", whichever is found first. If no version information is found in the manifest, an attempt is made to parse the file name. Returns a null is no version information is available.
toString( ) returns String
Returns the path to the jar file.