从命令行将JAR依赖项与可执行JAR(ÜberJAR)捆绑在一起 [英] Bundling JAR dependencies with executable JAR (Über JAR) from command line

查看:193
本文介绍了从命令行将JAR依赖项与可执行JAR(ÜberJAR)捆绑在一起的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从命令行创建一个可执行jar。 JAR中的主类具有已经打包到另一个普通JAR文件中的依赖项。

I'm trying to create an executable jar from the command line. The main class in the JAR has dependencies that i have packaged into another plain JAR file.

我想将依赖项JAR与可执行JAR一起打包以便拥有要发货的单个JAR文件。

I want to package the dependency JAR together with the executable JAR in order to have a single JAR file to ship.

到目前为止我所尝试的内容如下:

What i have tried so far is the following:

依赖项Hello。类文件具有模拟代码:

The dependency Hello.class file has the mock code:

public class Hello {
    public String getHello() {
        return "Well hello there!!";
    }
}

我使用以下方法将类文件打包到hello.jar中:

I have packaged class file into hello.jar using:

jar cvfM hello.jar Hello.class

hello.jar 内容现在是:

hello.jar -+- Hello.class

现在我的主类包含模拟代码:

Now i have the main class with the mock code:

public class Main {
    public static void main(String[] args) {
        System.out.println(new Hello().getHello());
    }
}

然后我创建一个清单文件清单。 txt 包含以下内容:

I then create a manifest file manifest.txt with the following content:

Main-Class: Main
Class-Path: hello.jar

我现在使用以下方法创建可执行JAR:

I now create the executable JAR using:

jar cvfm main.jar manifest.txt Main.class hello.jar

main.jar 内容现在是:

main.jar -+- Main.class
          |
          +- hello.jar
          |
          +- META-INF -+- MANIFEST.MF

使用以下命令运行可执行JAR:

Running the executable JAR using:

java -jar main.jar

我得到 Hello 类依赖项的类加载器错误。我理解这是因为类加载器在与 main.jar 相同的路径中查找 hello.jar 。因此,当我将 hello.jar 的副本与 main.jar 放在一起时,我可以运行它。

I get a class loader error for the Hello class dependency. I understand that this is because the class loader looks for hello.jar in the same path as main.jar. So when i put a copy of hello.jar alongside main.jar i am able to run it.

为了能够在其中运行带有hello.jar的 main.jar ,我需要做些什么?

What do i need to do in order to be able to run main.jar with hello.jar inside of it?

我知道你会问:他为什么要这样做呢?我知道ppl主要使用Eclipse,Ant,Maven或其他工具来做到这一点。但请只是幽默我:)

I know that you will ask: "why is he trying to do it this way?". I know that ppl mostly use Eclipse, Ant, Maven or other tools to do this. But please just humor me :)

推荐答案

遗憾的是,您的方法完全错误。
没有正常的方法将jar 放在另一个jar的里面。所以你的hello.jar在main.jar中无关!关键是普通类加载器不会在jar中查找jar文件,因此你会得到类找不到错误。
但是:如果你想要desparetly这样做,那么google为OneJar并转到 http:/ /one-jar.sourceforge.net/

Your approach is completely wrong unfortunately. There is no "normal" way to place a jar inside of another jar. So your hello.jar has nothing to do inside of main.jar! The point is the "normal" classloader wont look for jar files inside of jars, hence you get the class not found error. However: if you want desparetly to do that, then google for "OneJar" and go to http://one-jar.sourceforge.net/

这篇关于从命令行将JAR依赖项与可执行JAR(ÜberJAR)捆绑在一起的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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