SKP's Java/Java EE Gotchas: JAR File Dependencies

DZone 's Guide to

SKP's Java/Java EE Gotchas: JAR File Dependencies

If you plan on referring to JARs wrapped up in different JARs, don't do i through the Manifest's Class-Path. Move them to the same level or directory.

· Java Zone ·
Free Resource

You might find yourself running into a problem 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 that doesn't work.



|  |     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 shies away from your strongest belief/intuition, but it 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:


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.

class-path, jar dependency, jars, java, tutorial

Published at DZone with permission of Sumith Puri , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}