从命令行将 JAR 依赖项与可执行 JAR (Über JAR) 捆绑在一起
我正在尝试从命令行创建可执行 jar。 JAR 中的主类具有我已打包到另一个普通 JAR 文件中的依赖项。
我想将依赖项 JAR 与可执行 JAR 打包在一起,以便提供单个 JAR 文件。
到目前为止我已经尝试过以下内容:
依赖项 Hello.class 文件具有模拟代码:
public class Hello {
public String getHello() {
return "Well hello there!!";
}
}
我已使用以下方法将类文件打包到 hello.jar 中:
jar cvfM hello.jar Hello.class
hello.jar 内容现在是:
hello.jar -+- Hello.class
现在我有带有模拟代码的主类:
public class Main {
public static void main(String[] args) {
System.out.println(new Hello().getHello());
}
}
然后,我创建一个包含以下内容的清单文件 manifest.txt:
Main-Class: Main
Class-Path: hello.jar
我现在使用以下内容创建可执行 JAR:
jar cvfm main.jar manifest.txt Main.class hello.jar
main.jar 内容是现在:
main.jar -+- Main.class
|
+- hello.jar
|
+- META-INF -+- MANIFEST.MF
运行可执行 JAR 使用:
java -jar main.jar
我得到一个类加载器Hello 类依赖项错误。据我所知,这是因为类加载器在与 main.jar 相同的路径中查找 hello.jar 。因此,当我将 hello.jar 的副本放在 main.jar 旁边时,我就可以运行它。
我需要做什么才能运行其中包含 hello.jar 的 main.jar ?
我知道你会问:“他为什么要这样做?”。我知道人们大多使用 Eclipse、Ant、Maven 或其他工具来做到这一点。但请幽默一下我:)
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.
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:
The dependency Hello.class file has the mock code:
public class Hello {
public String getHello() {
return "Well hello there!!";
}
}
I have packaged class file into hello.jar using:
jar cvfM hello.jar Hello.class
The hello.jar content is now:
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());
}
}
I then create a manifest file manifest.txt with the following content:
Main-Class: Main
Class-Path: hello.jar
I now create the executable JAR using:
jar cvfm main.jar manifest.txt Main.class hello.jar
The main.jar content is now:
main.jar -+- Main.class
|
+- hello.jar
|
+- META-INF -+- MANIFEST.MF
Running the executable JAR using:
java -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.
What do i need to do in order to be able to run main.jar with hello.jar inside of it?
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 :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不幸的是,你的方法是完全错误的。
没有“正常”的方法可以将一个罐子放入另一个罐子中。所以你的 hello.jar 与 main.jar 内部没有任何关系!重点是“普通”类加载器不会在 jar 中查找 jar 文件,因此您会收到类未找到错误。
但是:如果您非常想这样做,请在 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 中,则必须编写自己的类加载器。
有几种产品已经为您提供了支持。 One-Jar 是我使用得非常成功的其中之一——可以轻松地编写它的脚本。
这是关于整个主题的有趣的讨论 -
将版本合并到一个 JAR 文件的最简单方法
There's no easy way to do this. That is, you are going to have to write your own classloader if you want to nest jars inside a jar.
There are several products out there that already support this for you. One-Jar is one of them that I've used with a lot of success -- can easily ant script it.
Here is an interesting SO discussion on the whole topic --
Easiest way to merge a release into one JAR file
好吧,我相信您可能面临着 jar 生成错误。查看此链接,它可能会给您带来启发。
预告片:
因此,您可以检查以下链接之一:
classpath-include-jar-within-a-jar,java-easiest-way-to-merge-a-发布到一个 jar 文件
干杯!
Well, I believe you may be facing a jar generation bug. Check out this link, it might enlight you.
A teaser:
Therefore, you may check one of the following links:
classpath-including-jar-within-a-jar, java-easiest-way-to-merge-a-release-into-one-jar-file
Cheers!