Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

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

Download Microservices for Java Developers: A hands-on introduction to frameworks and containers. Brought to you in partnership with Red Hat.

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.

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 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:

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.

Download Building Reactive Microservices in Java: Asynchronous and Event-Based Application Design. Brought to you in partnership with Red Hat

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

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

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}