java 是否可以有一个 jar 清单来使用文件夹中的所有 jar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39839891/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 04:41:12  来源:igfitidea点击:

Is it possible to have a jar Manifest to use all jars in a folder

javajardependenciesmanifest

提问by javydreamercsw

I'm trying to set a jar manifest so it loads all the libraries (jars) within a folder next to the jar.

我正在尝试设置一个 jar 清单,以便它在 jar 旁边的文件夹中加载所有库(jar)。

The manifest looks like this:

清单如下所示:

Manifest-Version: 1.0
Class-Path: libs/
Main-Class: path.to.my.class.Main

The layout is as follows:

布局如下:

- MyJar.jar
- libs/
-----lib1.jar
-----lib2.jar

And I'm trying to run like this:

我正在尝试像这样运行:

java -jar MyJar.jar

And I get NoClassDefinition errors about classes in the jar within the folder.

我在文件夹中的 jar 中收到有关类的 NoClassDefinition 错误。

In case someone is curious, this folder might contain optional jars that are processed during class loading. That' swhy I can't use the hardcoded or autogenerated ones.

如果有人好奇,此文件夹可能包含在类加载期间处理的可选 jar。这就是为什么我不能使用硬编码或自动生成的。

Any idea?

任何的想法?

UpdateRephrased the question as this is not currently possible from the manifest itself. The answer was the only really viable without the need of extracting the jars, although it also works.

更新 重新表述了这个问题,因为目前无法从清单本身中实现这一点。答案是唯一不需要提取罐子的真正可行的方法,尽管它也有效。

So as a summary the answer is no, this can't be done from manifest file alone if you have unespecified dependencies.

因此,总而言之,答案是否定的,如果您有未指定的依赖项,则无法仅从清单文件中完成此操作。

采纳答案by kevin

java does not know the jar files in the libs directory. If you are using java 1.6+, You can run program as

java不知道libs目录下的jar文件。如果您使用的是 java 1.6+,则可以将程序作为

java -cp lib/* -jar MyJar.jar

java -cp lib/* -jar MyJar.jar

回答by Issam EL-ATIF

You should define your Manifest classpath as

您应该将清单类路径定义为

Class-Path: libs/lib1.jar libs/lib2.jar

See Oracle documentation for more details https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

有关更多详细信息,请参阅 Oracle 文档https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

回答by lord.didger

Try extracting these jars. It looks like you cannot add all jars from directory but you can add all classes. You lose obviously all configuration in manifest, however, if you are interested in jars' code content only, it might work.

尝试提取这些罐子。看起来您无法从目录中添加所有 jar,但您可以添加所有类。您显然会丢失清单中的所有配置,但是,如果您只对 jars 的代码内容感兴趣,它可能会起作用。

I tested that with these simple classes

我用这些简单的类测试过

import pkg.B;

public class A {
  public static void main(String[] args) {
    System.out.println(B.class.getName());
  }
}

package pkg;

public class B {}

now I try to separate the classes. I have jarred them into

现在我尝试将课程分开。我已经把他们搅成一团

$ jar tf libA.jar 
META-INF/
META-INF/MANIFEST.MF
A.class
$ jar tf libB.jar 
META-INF/
META-INF/MANIFEST.MF
pkg/B.class

no Class-Path in any manifest. I can run A with java -cp libB.jar:libA.jar A. Now I create another jar with Class-Path set to lib/

任何清单中都没有 Class-Path。我可以用java -cp libB.jar:libA.jar A. 现在我创建另一个 jar 并将 Class-Path 设置为lib/

$ cat manifest 
Class-Path: lib/
$ jar cfm empty.jar manifest

my directory tree look like

我的目录树看起来像

$ ls -R
.:
A.java  empty.jar  lib  lib.jar  manifest  pkg

./lib:
libA.jar  libB.jar

./pkg:
B.java

Now I try jar

现在我尝试 jar

$ java -jar empty.jar 
Error: Could not find or load main class A

Hopeless, right? Then I extracted libA.jar and libB.jar into lib (same as [this guy][2]). Now all is fine

无望,对吧?然后我将 libA.jar 和 libB.jar 提取到 lib(与 [this guy] [2] 相同)。现在一切都很好

$ java -jar empty.jar 
pkg.B