Java Manifest.mf 类路径问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/332522/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java Manifest.mf classpath issues
提问by erickson
I've been trying to run a jar file - let's call it test.jar - that uses the Sybase jconn3.jar on a Unix system.
我一直在尝试运行一个 jar 文件——我们称之为 test.jar——它在 Unix 系统上使用 Sybase jconn3.jar。
I have created a MANIFEST.MF file that has the following:
我创建了一个具有以下内容的 MANIFEST.MF 文件:
Class-Path: $SYBASE/jConnect-6_0/classes/jconn3.jar commons-net-1.3.0.jar
This gives a ClassNotFoundError. $SYBASE is the system variable that points to /opt/sybase13; I've also tried the following:
这给出了一个 ClassNotFoundError。$SYBASE 是指向 /opt/sybase13 的系统变量;我还尝试了以下方法:
Class-Path: /opt/sybase13/jConnect-6_0/classes/jconn3.jar commons-net-1.3.0.jar
and
和
Class-Path: opt/sybase13/jConnect-6_0/classes/jconn3.jar commons-net-1.3.0.jar
However, if I copy the jconn3.jar file from the $SYBASE/jConnect-6_0/classes to the same directory as test.jar, and update my MANIFEST.MF to read as follows:
但是,如果我将 jconn3.jar 文件从 $SYBASE/jConnect-6_0/classes 复制到与 test.jar 相同的目录,并将我的 MANIFEST.MF 更新为如下所示:
Class-Path: jconn3.jar commons-net-1.3.0.jar
The application runs as expected.
应用程序按预期运行。
Now, I've been able to verify the jconn3.jar file works by copying it locally; my MANIFEST.MF file includes the path to my Main-Class, so that's not at issue here.
现在,我已经能够通过在本地复制来验证 jconn3.jar 文件是否有效;我的 MANIFEST.MF 文件包括我的主类的路径,所以这不是问题。
What do you think could be the problem? I've been looking at this thing for too long now. Thanks!
你认为可能是什么问题?我已经看这个东西太久了。谢谢!
回答by erickson
The entries in the class-path are either relative to the JAR in which they are embedded (which you have working) or are URLs. To make your absolute paths work, you'll need to convert them to URLs, e.g.,
类路径中的条目要么相对于嵌入它们的 JAR(您使用的 JAR)要么是 URL。为了使您的绝对路径工作,您需要将它们转换为 URL,例如,
file:/opt/sybase13/...
file:/opt/sybase13/...
There's no mechanism for using variables.
没有使用变量的机制。
Although the JAR specification doesn't say it clearly, absolute file:scheme URLs do work in the class-path attribute.
尽管 JAR 规范没有明确说明,但绝对file:方案 URL 在 class-path 属性中确实有效。
回答by OscarRyz
Environment variables are not readed by the classloader AFAIK. However you could add the jar in a configuration script
类加载器 AFAIK 不会读取环境变量。但是,您可以在配置脚本中添加 jar
Accoding to the specification the entries are relatives to the jar not absolute:
根据规范,条目是相对于 jar 的而不是绝对的:
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.
类路径:
此属性的值指定此应用程序或扩展程序所需的扩展程序或库的相对 URL。URL 由一个或多个空格分隔。应用程序或扩展类加载器使用此属性的值来构建其内部搜索路径。
http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Manifest Specification
http://java.sun.com/j2se/1.5.0/docs/guide/jar/jar.html#Manifest 规范

