java 如何创建具有依赖项 jar 的可执行 jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1491990/
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
How to create an executable jar with dependency jars
提问by Sunil Kumar Sahoo
I have created a java application which depends upon some external jars. Now I want to create an executable jar for my project. Which means if I double click the project then it should execute.
我创建了一个依赖于一些外部 jar 的 java 应用程序。现在我想为我的项目创建一个可执行的 jar。这意味着如果我双击该项目,那么它应该执行。
采纳答案by kgiannakakis
You can do that easily with Ant:
你可以用 Ant 轻松做到这一点:
<jar jarfile="MyJar.jar" basedir="bin">
<manifest>
<attribute name="Class-Path" value="lib/lib1.jar lib/lib2.jar lib/lib3.jar"/>
<attribute name="Built-By" value="me"/>
<attribute name="Main-Class" value="mypackage.Myclass"/>
</manifest>
</jar>
This will add all the appropriate entries to the Manifest file. In order to be able to run the jar, you also need to create a lib folder and place all the dependency jars there:
这会将所有适当的条目添加到清单文件中。为了能够运行 jar,您还需要创建一个 lib 文件夹并将所有依赖 jar 放在那里:
myjar.jar
lib/lib1.jar
lib/lib2.jar
lib/lib3.jar
回答by Maciej Kreft
Use eclipse plugin called "fatjar"
使用名为“fatjar”的eclipse插件
it's update-site
这是更新站点
http://kurucz-grafika.de/fatjar
http://kurucz-grafika.de/fatjar
Just right-click on project and use fatjar option, next step allow you to choose which library will be included in *.jar
只需右键单击项目并使用 fatjar 选项,下一步允许您选择将包含在 *.jar 中的库
回答by developmentalinsanity
回答by Martin McNulty
You will need to add a MANIFEST.MF file to the JAR for your application, (under the META-INF directory - if you use the 'jar' command line tool it will make sure the file ends up in the right place). It will need to contain two attributes (at least):
您需要将 MANIFEST.MF 文件添加到应用程序的 JAR 中(在 META-INF 目录下 - 如果您使用“jar”命令行工具,它将确保该文件最终位于正确的位置)。它将需要包含两个属性(至少):
- Main-Class: [the fully qualified name of the class in your app that has a main method]
- Class-Path: [the list of JAR dependencies for your application]
- Main-Class:[应用程序中具有 main 方法的类的完全限定名称]
- 类路径:[应用程序的 JAR 依赖项列表]
More details on manifest files in JAR files can be found here: http://java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
有关 JAR 文件中清单文件的更多详细信息,请访问:http: //java.sun.com/docs/books/tutorial/deployment/jar/manifestindex.html
If you're using a build tool like Apache Mavenyou might find that it is able to generate this for you.
如果您正在使用像Apache Maven这样的构建工具,您可能会发现它能够为您生成它。
回答by Jim Downing
If you use Maven the assembly plugin will do this for you very simply: http://maven.apache.org/plugins/maven-assembly-plugin/howto.html
如果您使用 Maven,程序集插件将非常简单地为您完成此操作:http: //maven.apache.org/plugins/maven-assembly-plugin/howto.html
Otherwise you'll need to follow the instructions in the JAR file tutorial: http://java.sun.com/docs/books/tutorial/deployment/jar/index.htmland creating a manifest file including your main class as Main-Class: [classname]and listing your external jars as Class-Path: theirJar1 theirJar2etc.
否则,您需要按照 JAR 文件教程中的说明进行操作:http: //java.sun.com/docs/books/tutorial/deployment/jar/index.html并创建一个清单文件,其中包括您的主类Main-Class: [classname]和清单你的外部罐子Class-Path: theirJar1 theirJar2等。

