Saturday, April 18, 2015

JAR File Dependencies (Internal References/Class-Path)

Most often, you end up in the following issue - where you want to refer to a JAR inside another JAR. Many developers would assume that by adding it to the Class-Path within MANIFEST.MF, it should be included in the classpath. But this does not work.

test.jar
|  /META-INF
|  |     MANIFEST.MF
|  |        Main-Class: com.persistent.accelerite.main.AppLauncher

|  |        Class-Path: commons-logging.jar
|  /com/persistent/accelerite/main
|  |     AppLauncher.class
|  commons-logging.jar


This may be very surprising or something that will be away from your strongest belief/intuition - But this will not work! You have to extract the required JARs and put them at the same level or same directory as the directory where test.jar is located. In the above class, trying to use jar:file:jarname.jar!/commons-logging.jar in the Class-Path will also not work. The only other way to achieve this is to write Custom Class Loaders.

This is as per the Java Archive Specification.  From the official Oracle Documentation is as follows:

Class-Path:
The value of this attribute specifies the relative URLs of the extensions or libraries that this application or extension needs. URLs are separated by one or more spaces. The application or extension class loader uses the value of this attribute to construct its internal search path.

No comments: