How to set different path in build.xml

15,119

Based on some hints from the links I have provided in my comments, you can try this (which I haven't tried):

<target depends="init" name="build-projects">
    <property name="myproject.root.path" location="d:/ext_jars"/>

    <path id="class.path">
        <fileset dir="${myproject.root.path}">
            <include name="*.jar"/>
            <exclude name="${ant.project.name}.jar"/>
        </fileset>
    </path>

    <manifestclasspath property="jar.classpath" jarfile="${myproject.root.path}/${ant.project.name}.jar">
        <classpath refid="class.path"/>
    </manifestclasspath>

    <jar destfile="${myproject.root.path}/${ant.project.name}.jar" basedir="classes/app" excludes="*.properties">
        <manifest> 
            <attribute name="Main-Class" value="path.to.my.MainClass" />
            <attribute name="Class-Path" value="${jar.classpath}"/>
        </manifest>
    </jar>
</target>

Example MANIFEST.MF generated by Ant task:

Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.6.0_29-b11 (Sun Microsystems Inc.)
Main-Class: path.to.my.MainClass
Class-Path: Filters.jar RXTXcomm.jar collections-generic-4.01.jar colt
 -1.2.0.jar commons-codec-1.5.jar commons-io-2.0.1.jar commons-validat
 or-1.3.1.jar concurrent-1.3.4.jar forms-1.3.0.jar gdal.jar geotiff-ja
 i.jar jai_imageio-1.1.jar jakarta-oro-2.0.1.jar java-image-scaling-0.
 8.5.jar jcommander-1.7.jar jgroups-2.12.1.Final.jar jna.jar jung-algo
 rithms-2.0.1.jar jung-api-2.0.1.jar jung-graph-impl-2.0.1.jar jung-vi
 sualization-2.0.1.jar nimrodlf-1.2.jar platform.jar stax-api-1.0.1.ja
 r swingx-beaninfo-1.6.2.jar swingx-core-1.6.2.jar vecmath-1.3.1.jar v
 lcj-1.2.0.jar wstx-asl-3.2.6.jar

As you can see all external jar libraries listed in the Class-Path follow the exact name of the jars that the main application will use. All of them must be at the same file path as the main application's jar file path. The main application's class (usually with the main() method) will be called from the Main-Class of the MANIFEST.MF and its jar must not be included in the Class-Path. If everything is correct, the jar will be runnable.

Don't worry about the weird formatting of the MANIFEST.MF; it is generated in 80-column text style by the Ant task.

Share:
15,119
Amit
Author by

Amit

Updated on June 04, 2022

Comments

  • Amit
    Amit almost 2 years

    I want to put path of different directory (which contains jars, presently on d:/ext_jars). After running the build.xml. I want that path should take that runnable jar.

    Currently that jar is giving me errors because of classpath is not present (which was previously under c:/project/lib/*.jars, converted into D:/ext_jars).

    Please help me, that how can I set that external directory in classpath of build.xml?


    My code in : build.xml

    <?xml version="1.0"?>
    <project name="MyProject" default="deploy" basedir="." >
        <property file="manifest.mf" />
        <property name="dir.src" value="src" />
        <property name="dir.build" value="bin" />
        <property name="dir.dist" value="dist/MyProject" />
        <property name="dir.lib" value="lib" />
    <!-- Creates the output directories -->
        <target name="prepare">
            <mkdir dir="${dir.build}" />
            <mkdir dir="${dir.dist}" />
    
            <mkdir dir="${dir.dist}/${dir.csvdata}" />
            <mkdir dir="${dir.dist}/${dir.MH}" />
        </target>
    
        <target name="clean" description="Remove all generated files.">
            <delete dir="${dir.build}" />
            <delete dir="${dir.dist}" />
        </target>
    
        <path id="build.classpath">
            <fileset dir="${dir.lib}">
                <include name="**/*.jar" />
                <include name="**/*.zip" />
            </fileset>
        </path>
        <path id="build.classpath.ref">
            <fileset dir="D:/ext_jars">
                <include name="**/*.jar" />
                <include name="**/*.zip" />
            </fileset>
        </path>
    
        <target name="compile" depends="prepare" description="Compile all source code.">
            <!--<javac srcdir="${dir.src}" destdir="${dir.build}" debug="true">-->
            <javac  destdir="${dir.build}" debug="true">
                <classpath refid="build.classpath" />
                <classpath refid="build.classpath.ref" />
                <src path="src"/>           
            </javac>        
            <copy todir="${dir.build}">
                <fileset dir="${dir.src}">
                    <exclude name="**/*.java" />
                </fileset>
            </copy>     
        </target>
    
        <pathconvert property="classpath" refid="build.classpath">
        </pathconvert>
        <pathconvert property="classpath" refid="build.classpath.ref">
        </pathconvert>
    
        <target name="jar" depends="compile" description="Generates ${project.name}.Jar in the 'dist' directory.">
            <jar jarfile="${dir.dist}/${ant.project.name}.jar">
                <fileset dir="${dir.build}" includes="**/*.*" />
                <manifest>
                    <attribute name="Class-Path" value="${Class-Path}" />
                    <attribute name="Main-Class" value="${Main-Class}" />
                </manifest>
            </jar>
        </target>
        <target name="deploy" depends="clean,jar">
            <copy todir="${dir.dist}/${dir.lib}">
                <fileset dir="${dir.lib}">
                    <include name="**/*.jar" />
                    <include name="**/*.zip" />
                </fileset>
            </copy>
        </target>
    

    Using this code After runing project it is running well, But after running jar created by this code it is not running it is throughing error as follows:

    Exception in thread "main" java.lang.NoClassDefFoundError: dowlibpkg/DowLib
            at mypkg.MainApp.main(MainApp.java:109)
    Caused by: java.lang.ClassNotFoundException: dowlibpkg.DowLib
            at java.net.URLClassLoader$1.run(Unknown Source)
            at java.security.AccessController.doPrivileged(Native Method)
            at java.net.URLClassLoader.findClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
            at java.lang.ClassLoader.loadClass(Unknown Source)
            ... 1 more
    

    SO created Jar is not running../ I want it to be run, can any one please help me

    ==============================================================

    manifest.mf

    Manifest-Version: 1.0 Class-Path: lib/org.springframework.asm-3.0.1.RELEASE \ lib/org.springframework.beans-3.0.1.RELEASE \ lib/org.springframework.context-3.0.1.RELEASE \ lib/org.springframework.core-3.0.1.RELEASE \ lib/org.springframework.expression-3.0.1.RELEASE \ lib/org.springframework.oxm-3.0.1.RELEASE \ lib/org.springframework.web-3.0.1.RELEASE Main-Class: myProject.MainApp