Creating an independent jar file

家住魔仙堡 提交于 2019-12-01 18:38:28
Roman C

You can use an ant script to build the runnable JAR.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project default="create_run_jar" name="Create Runnable Jar for Project SimpleApp with libraries in sub-folder">
    <!--ANT 1.7 is required -->
    <target name="create_run_jar">
        <jar destfile="C:/Workspaces/SimpleApp/SimpleApp.jar">
            <manifest>
                <attribute name="Main-Class" value="SimpleApp"/>
                <attribute name="Class-Path" value=". SimpleApp_lib/lib1.jar SimpleApp_lib/lib2.jar"/>
            </manifest>
            <fileset dir="C:/Workspaces/SimpleApp/bin"/>
        </jar>
        <delete dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <mkdir dir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib1.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
        <copy file="C:/path/to/lib/lib2.jar" todir="C:/Workspaces/SimpleApp/SimpleApp_lib"/>
    </target>
</project>

In this example, the project's SimpleApp depends on two libs: lib1.jar and lib2.jar, which are output to bin with a MANIFEST.MF having the attributes specified.

Mark O'Connor

The following posting has a brief explanation of how to create an executable jar using ANT:

This posting explains how the manifestclasspath task can assist with creating the classpath manifest entry (making the construction of executable jars more robust and less error prone):

Finally a more complex example demonstrating the use of ivy to manage your project's 3rd party dependencies when creating an executable jar:

Simply You can create a JAR-file by executing following command:

jar -c excel.jar MANIFEST.MF *.class

The MANIFEST.MF-file should contain following line:

Main-Class: createExcel

But consider following tips too :

There are several ways:

  1. Create a jar file and put your classes (without dependencies) there. Use some tool (any IDE has it) to do this and specify class with main function. You can also do it manually from command-line. When user want to run it he should specify classpath and all dependencies should be in that classpath.

  2. Create the same jar and create .bat or .sh file in which set classpath and run your jar.

  3. Create cross-platform installer with some special tool (but good tools aren't free).

Remember that Netbeans can help you a lot ;)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!