Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

JarManifestUtilities



The JarManifestUtilities class allows to check that the jar files declared in the Class-Path attribute in a Jar file Manifest exist. There are two ways to use this class:
  • A basic way allowing to get the list of Jar files referenced in the Manifest
  • A simpler way allowing to check that the Jar files referenced in the Manifest are those intended to be references

Getting the list of Jar files referenced in the Manifest

The JarManifestUtilities.getDependencies(...) methods allow to get the list of Jar files referenced in the Class-Path property of a Jar file Manifest. It will return null if there is no Manifest in the Jar File. For example:
  • The JarManifestUtilities.getDependencies(URL) return the list of direct dependencies referenced in the Class-Path property of the Jar file[1]
    "direct" means that only Jar files referenced in the Class-Path property of the Jar file will be taken into account
  • The JarManifestUtilities.getDependencies(URL, boolean) return the list of direct or indirect dependencies referenced directly or indirectly[2]
    "indirect" means that Jar files referenced in the Manifest property of Jar file referenced in the initual jar file will be taken into account
    will be taken into account

Example

Suppose the C:/my/directory/myJarFile.jar jar file with the following Manifest:
   Class-Path: lib/theSupportLib.jar \
   lib/theSecondLib.jar
The:
   List<URL> list = JarManifestUtilities.getDependencies(<myJarFile>);
Will return a list with two URLs:
  • C:/my/directory/lib/theSupportLib.jar
  • C:/my/directory/lib/theSecondLib.jar

Checking the dependencies referenced in the Manifest

The JarManifestUtilities.checkDependencies(...) methods allow to check that the Jar files referenced in the Class-Path property of a Jar file Manifest exist.

The simpler of these methods are: Other methods allow to take into account the cases where the structure of Jar files in a project are different from the one defined in the deployed library:

Example

Suppose the C:/my/project/dist/myJarFile.jar jar file with the following Manifest:
   Class-Path: lib/theSupportLib.jar \
   lib/theSecondLib.jar
Suppose that we use an IDE where the libraries have been put in the c:/my/project/lib/ directory, and the user dir is at the root of the project.

The following code will check that the Jar files declared in the Manifest exist:
   File lib = new File(System.getLibrary("user.dir"), "lib");
   URL libURL = lib.toURI().toURL(); 
   boolean check = JarManifestUtilities.getDependencies(<myJarFile>, libURL, null);
If the lib directory contains only theSupportLib.jar and theSecondLib.jar, the method will return true. If some Jar files are not present, or there are more, if will return false.

Second example

Now consider the same lib directory with the following jar files:
  • theSupportLib.jar
  • theSecondLib.jar
  • anotherLib.jar
The following code will return false bevcause there is ony more jar file in the lib directory:
   File lib = new File(System.getLibrary("user.dir"), "lib");
   URL libURL = lib.toURI().toURL(); 
   boolean check = JarManifestUtilities.getDependencies(<myJarFile>, libURL, null);
But it is possible to exclude the anotherLib.jar jar file with the following code:
   File lib = new File(System.getLibrary("user.dir"), "lib");
   URL libURL = lib.toURI().toURL(); 
   Set<String> excluded = new HashSet<>();
   sexcluded.add("anotherLib.jar");
   boolean check = JarManifestUtilities.getDependencies(<myJarFile>, libURL, excluded);
In that case the method will return true.

Using more basic methods to perform the check

Several methods allow to simplify the check if you are using Files. For example: For example:
   File lib = new File(System.getLibrary("user.dir"), "lib");
   boolean check = JarManifestUtilities.getDependenciesFromFiles(<myJarFile>, lib);

Configurating the behavior

There are two static fields in the class which allow to configure how the class will work:

Examples

First example

Suppose the following a jar file with the following Manifest:
   Class-Path: lib/theSupportLib.jar \
   lib/theSecondLib.jar
And the following project structure:
      dist
      - myJarFile.jar
      nbproject
      lib
      - theFirstLib.jar      
      - theSecondLib.jar
      build.xml
If we perform the following code:
   File userdir = new File(System.getLibrary("user.dir"));
   File dist = new File(userdir, "dist");
   File lib = new File(userdir, "lib");
   File myJarFile = new File(dist, "myJarFile.jar");
   boolean check = JarManifestUtilities.getDependenciesFromFiles(<myJarFile>, lib);
The getDependenciesFromFiles method will return true.

Second example

Suppose the same jar file with the following project structure:
      dist
      - myJarFile.jar
      nbproject
      lib     
      - theSecondLib.jar
      build.xml
If we perform the following code:
   File userdir = new File(System.getLibrary("user.dir"));
   File dist = new File(userdir, "dist");
   File lib = new File(userdir, "lib");
   File myJarFile = new File(dist, "myJarFile.jar");
   boolean check = JarManifestUtilities.getDependenciesFromFiles(<myJarFile>, lib);
The getDependenciesFromFiles method will return false, with the following error message:
The theSecondLib.jar file present in the Class-Path do not exist in the expected files list

Third example

Suppose the same jar file with the following project structure:
      dist
      - myJarFile.jar
      nbproject
      lib     
      - theFirstLib.jar          
      - theSecondLib.jar
      build.xml
If we perform the following code:
   JarManifestUtilities.ALLOW_UNUSED_EXPECTED_FILES = false;
   File userdir = new File(System.getLibrary("user.dir"));
   File dist = new File(userdir, "dist");
   File lib = new File(userdir, "lib");
   File myJarFile = new File(dist, "myJarFile.jar");
   boolean check = JarManifestUtilities.getDependenciesFromFiles(<myJarFile>, lib);
The getDependenciesFromFiles method will return false, with the following error message:
The theFirstLib.jar expected file is not present in the Class-Path

Notes

  1. ^ [1] [2] "direct" means that only Jar files referenced in the Class-Path property of the Jar file will be taken into account
  2. ^ "indirect" means that Jar files referenced in the Manifest property of Jar file referenced in the initual jar file will be taken into account

Categories: General

Copyright 2019 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 licence

Project Web Hosted by SourceForge.net Copyright 1999-2010 - Geeknet, Inc., All Rights Reserved About - Legal - Help