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

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

Public Methods

getFile( ) returns java.io.File
Returns a java.io.File representation of the jar file or directory where the jar file has been extracted.
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.
getEntries( ) returns Entry[]
Used to return a list of all the entries found in the jar file.
getEntry( String Entry ) returns Entry
Used to retrieve a single entry from the jar file.
getEntry( String Package, String Entry ) returns 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").
getEntry( java.lang.Class Class ) returns Entry
Used to retrieve a single entry from the jar file.
getClasses( ) returns Class[]
Returns all the classes in the jar file. Returns an empty array if the jar file has not been loaded or if there are no classes in the file.
toString( ) returns String
Returns the path to the jar file.

Static Methods

getJars( java.lang.Package Package ) returns 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 Jar[]

Public Classes