简体   繁体   English

如何使用依赖项jar创建可执行jar

[英]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): 它需要包含两个属性(至少):

  • Main-Class: [the fully qualified name of the class in your app that has a main method] Main-Class:[应用程序中具有main方法的类的完全限定名称]
  • Class-Path: [the list of JAR dependencies for your application] Class-Path:[应用程序的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 Maven you might find that it is able to generate this for you. 如果你正在使用像Apache Maven这样的构建工具,你可能会发现它能够为你生​​成这个。

You want to set the Class-Path attribute in your JAR's Manifest file. 您想在JAR的Manifest文件中设置Class-Path属性。

This page should give you a good starting point. 这个页面应该给你一个很好的起点。

看看使用诸如IzPack之类的包装工具或诸如JSmooth之类的包装工具

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.

相关问题 如何从可执行jar引用Maven依赖jar? - How to refer maven dependency jars from an executable jar? 我如何使用Ant来构建单个可执行文件.jar,该文件在.jar的/ lib目录中具有依赖项.jars? - How can I use Ant to build a single executable .jar that has dependency .jars in it in a /lib dir in the .jar? Java:使用嵌入的依赖jar创建jar可执行文件 - Java : create jar executable with dependant jars embedded 具有依赖性的可执行jar使用maven而不解压缩jar但包含classpath - Executable jar with dependency using maven without unpacking jars but include classpath Maven插件创建可执行jar,依赖项未解包(带jar的jar) - Maven plugin to create executable jar with dependencies not unpacked (jar with jars) 创建可执行 Jar,其中包含所有依赖项作为打包的 jar - Create Executable Jar that includes all the dependencies as packaged jars 使用外部 jar 从命令行创建可执行 jar - create executable jar from command line with external jars 如何获取与依赖的jar捆绑在一起的Executable javafx jar? - How to get Executable javafx jar bundled with dependent jars? 如何使用外部文件夹和Jar创建Jar文件 - How to create Jar file with external folders and Jars 如何创建可执行的jar文件夹? - How to create an executable jar folder?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM