创建JAR文件而不包含外部依赖项 [英] Create JAR file without including external dependencies

查看:151
本文介绍了创建JAR文件而不包含外部依赖项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以创建需要外部依赖关系的JAR文件,而不在JAR文件中包含这些依赖关系?

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

我的google-fu未能给我答案;我发现的所有内容都显示了如何将它们包含在JAR文件中,但是没有在清单文件中放置什么来说我还没有它们,请查看用户的类路径。我假设在用户的类路径上正确安装和配置了依赖项。

My google-fu has failed to give me an answer; everything that I have 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.

在我的例子中,我的依赖项是Apache Commons CLI和Math。

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

编辑:
在我的JAR文件中,我有Main.class。

Inside my JAR file, I have Main.class.

我的清单文件如下:

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

我的CLASSPATH看起来像

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

如果我在 / lib 中的JAR中包含依赖项并添加行 Class-Path:lib / commons-math-2.0.jar lib / commons-cli-1.2.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.

我尝试将 Class-Path:commons-math-2.0.jar commons-cli-1.2.jar 添加到清单中而不包含文件在JAR只是为了看看是否会起作用,但事实并非如此。

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.

推荐答案

使用 META-INF / MANIFEST.MF 中的> Class-Path 条目,告诉您在哪里查找相对于JAR的依赖项。例如:

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

这篇关于创建JAR文件而不包含外部依赖项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆