How to build an executable jar file with Ant

2019-07-12 17:54发布

I'm developing Swing based application in Java I want a executable JAR file for this project.

All external library files used in the project should be packaged in the JAR file.

How can I build a runnable JAR file using ANT?

标签: java ant
2条回答
Melony?
2楼-- · 2019-07-12 18:16

You need to include the manifest task in your jar task. The manifest references the main class to be executed by default when you start your jar.

it could look like this :

<jar...
<manifest file="MANIFEST.MF">
    <attribute name="Main-Class" value="com.example.YourMainClass"/>
</manifest>
</jar>
查看更多
贪生不怕死
3楼-- · 2019-07-12 18:20

but it needs all external library files used in the project should be along with the jar.

Of course, but the external JARS should not be bundled in with the executable JAR.

You need to do three things:

  1. Create a manifest for your executable JAR that specifies the Main-Class.
  2. Add the CLASSPATH to you manifest that spells out the location of each and every dependent JAR relative to the executable JAR.
  3. Create a ZIP file that contains the executable JAR and all the dependent JARs, with paths relative to the executable JAR as specified in your manifest.

You give clients the ZIP. They unpack it and execute your executable JAR.

查看更多
登录 后发表回答