[英]How to create an executable jar with dependency jars
I have created a java application which depends upon some external jars. 我创建了一个依赖于一些外部jar的java应用程序。 Now I want to create an executable jar for my project.
现在我想为我的项目创建一个可执行jar。 Which means if I double click the project then it should execute.
这意味着如果我双击项目,那么它应该执行。
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. 这将向Manifest文件添加所有适当的条目。 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文件夹并将所有依赖项放在那里:
myjar.jar
lib/lib1.jar
lib/lib2.jar
lib/lib3.jar
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中的库
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). 您需要将MANIFEST.MF文件添加到您的应用程序的JAR中(在META-INF目录下 - 如果您使用'jar'命令行工具,它将确保文件最终位于正确的位置)。 It will need to contain two attributes (at least):
它需要包含两个属性(至少):
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 Maven you might find that it is able to generate this for you. 如果你正在使用像Apache Maven这样的构建工具,你可能会发现它能够为你生成这个。
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.html and creating a manifest file including your main class as Main-Class: [classname]
and listing your external jars as Class-Path: theirJar1 theirJar2
etc. 否则,您需要按照JAR文件教程中的说明操作: http : //java.sun.com/docs/books/tutorial/deployment/jar/index.html并创建一个清单文件,包括您的主类作为
Main-Class: [classname]
并将您的外部jar列为Class-Path: theirJar1 theirJar2
等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.