在清单文件中定义外部 jar 的正确方法

Proper way of defining external jar in manifest files

我很难找到如何在 manifest.mf 中定义外部 jar。当我使用如下所示的 class 路径时,jar 文件没有被执行

Manifest-Version: 1.0
Sealed: true
Main-Class: org.test.MainClass
Class-Path: /Program Files (x86)/Testfolder/testlibrary/hibernate3.jar
 /Program Files (x86)/Testfolder/testlibrary/org.springframework.web-3.1.1.RELEASE.jar

当我给出如下所示的正确完整 class 路径时,未找到或加载主要 class

Manifest-Version: 1.0
Sealed: true
Main-Class: org.test.MainClass
Class-Path: D:/Program Files (x86)/Testfolder/testlibrary/hibernate3.jar
 D:/Program Files (x86)/Testfolder/testlibrary/org.springframework.web-3.1.1.RELEASE.jar

有人知道我做错了什么吗?

根据 documentation:

Class-Path Attribute

The manifest for an application can specify one or more relative URLs referring to the JAR files and directories for other libraries that it requires. These relative URLs are treated relative to the code base from which the containing application was loaded.

An application (or, more generally, a JAR file) specifies the relative URLs of the libraries that it requires with the manifest attribute Class-Path. This attribute lists the URLs to search for implementations of other libraries if they cannot be found on the host Java virtual machine. These relative URLs may include JAR files and directories for any libraries or resources needed by the application. Relative URLs not ending with a slash (/) are assumed to refer to JAR files. For example:

Class-Path: servlet.jar infobus.jar acme/beans.jar images/

At most one Class-Path header may be specified in a JAR file's manifest.

Currently, the URLs must be relative to the code base of the JAR file for security reasons. Thus, remote optional packages will originate from the same code base as the application.

Each relative URL is resolved against the code base from which the containing application or library was loaded. If the resulting URL is invalid or refers to a resource that cannot be found, then it is ignored.

我发现我也可以定义绝对路径为

Class-Path: file:///D:/Program%20Files%20(x86)

这对我来说效果很好。有关 main class not found 的错误与 class 路径中的错误有关,如上所示使用绝对路径将解决问题。