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.

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