开发者

Create JAR file without including external dependencies

开发者 https://www.devze.com 2022-12-20 08:16 出处:网络
Is it possible to create a JAR file that requires external dependencies without including those dependencies in the JAR file?

Is it possible to create a JAR file that requires external dependencies without including those dependencies in the JAR file?

My google-fu has failed to give me an answer; everything that I 开发者_如何学JAVAhave found shows how to include them in the JAR file, but not what to put in the manifest file to say "I haven't got them, look in the user's classpath". I would assume that the dependencies are properly installed and configured on the user's classpath.

In my case, my dependencies are Apache Commons CLI and Math.


Edit: Inside my JAR file, I have Main.class.

My manifest file looks like:

Manifest-Version: 1.0
Created-By: 1.6.0 (Sun Microsystems Inc.)
Main-Class: Main

My CLASSPATH looks like

.;C:\Program Files\Java\jre1.6.0_06\lib\ext\QTJava.zip;C:\java_lib\commons-cli-1.2.jar;C:\java_lib\commons-math-2.0\commons-math-2.0.jar

If I include the dependencies in the JAR in /lib and add the line Class-Path: lib/commons-math-2.0.jar lib/commons-cli-1.2.jar to the manifest, then it does work.

I've tried adding Class-Path: commons-math-2.0.jar commons-cli-1.2.jar to the manifest without including the files in the JAR just to see if that would work, but it didn't.


Use the Class-Path entry in the META-INF/MANIFEST.MF to tell where to look for dependencies relatively to your JAR. For example:

Class-Path: servlet.jar ../foo/bar.jar acme/beans.jar


It certainly is possible. One way to think about it is that every time you create a jar, you are depending on the classes in the jre, and it is not necessary to include them in your jar. The jre will automatically look for them in the classpath. If they are not found you will see a NoClassDefFoundError.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号