15

I have the following build.xml:

<project>

<target name="clean">
    <delete dir="./build"/>
</target>

<target name="compile">
    <mkdir dir="./build/classes"/>          
    <javac srcdir="./src" destdir="./build/classes"/>                   
</target>

<target name="jar">
    <mkdir dir="./build/jar"/>
    <jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes">
        <manifest>
            <attribute name="DependencyFinder" value="main"/>
        </manifest>
    </jar>
</target>

<target name="run">
    <java jar="./build/jar/DependencyFinder.jar" classname="${main-class}" fork="true"/>                    
</target>

</project>

Here is my directory structure:

/build /lib /MagicFolder /Src /build.xml

Folder src contains .java files.

Path to MagicFolder should be an input parameter.

lib has external .jar library which should be included in my build.

build folder which will have compiled .jar and.class` files

QUESTION: How should I change my build.xml? My build.xml should:

  1. Add external lib ./lib/jbl.jar
  2. When I run my application put 2 input parametrs for my application
gvlasov
  • 18,638
  • 21
  • 74
  • 110
pipsik
  • 219
  • 1
  • 2
  • 9

3 Answers3

45

If you need to add a jar to classpath to compile the code (sorry, it isn't quite clear what you're asking for), then you need to change <javac> task to look like this:

<javac srcdir="./src" destdir="./build/classes">   
    <classpath>
        <pathelement path="lib/jbl.jar"/>
    </classpath>
</javac>

Or if you need to add contents of jbl.jar to the jar you are creating, then you need to change your <jar> task to look like this:

<jar destfile="./build/jar/DependencyFinder.jar" basedir="./build/classes>
    <zipgroupfileset dir="lib" includes="jbl.jar" />
    <manifest>
        <attribute name="DependencyFinder" value="main"/>
        <attribute name="Main-Class" value="org.ivanovpavel.YourMainClass"/>
    </manifest>
</jar>

To add arguments to <java> call, use nested <arg> elements:

<target name="run">
    <java jar="./build/jar/DependencyFinder.jar:lib/jbl.jar" classname="dependencyfinder.DependencyFinder">  
        <arg value="Alexander Rosenbaum"/>
        <arg value="Dmitry Malikov"/>
    </java>                  
</target>
gvlasov
  • 18,638
  • 21
  • 74
  • 110
  • When I'm start run task, I'm have error message: Cannot user 'jar' and 'classname' attributes in same command – pipsik Oct 29 '13 at 12:57
  • Big thanks! Could you help me with one more problem here? I'm have problems with run task. I got message after execute: [java] java.lang.NoClassDefFoundError: org/ooLab/language/javaByteCode/JavaByteCodeException bla bla bla Caused by: java.lang.ClassNotFoundExecption: org.ooLab.language.JavaByteCode.JavaByteCodeException all classes of ooLab place in external jar lib ./lib/jbl.jar – pipsik Oct 29 '13 at 13:45
  • Could you change code in your comment? Of course I'm will set accept :) good example of input paramentrs – pipsik Oct 29 '13 at 14:53
  • Your change didnt helps me :( – pipsik Oct 29 '13 at 15:58
  • Didnt helps :( – pipsik Oct 29 '13 at 16:08
  • Errors are the same: [java] java.lang.NoClassDefFoundError: org/ooLab/language/javaByteCode/JavaByteCodeException bla bla bla Caused by: java.lang.ClassNotFoundExecption: org.ooLab.language.JavaByteCode.JavaByteCodeException http://cs320621.vk.me/v320621781/4f11/mvr7KLULtL0.jpg – pipsik Oct 29 '13 at 16:15
  • http://cs320621.vk.me/v320621781/4f1a/s3u7vtWVpj8.jpg I'm very nervous now. Tomorrow - deadline :( – pipsik Oct 29 '13 at 16:19
4

There are two ways to run a java program. Using the "jar" option is the most convenient and is called an executable jar, however in order to make it work you need to specify both the Main class and classpath in the manifest file as follows:

<jar destfile="${jar.file}" basedir="${classes.dir}">
    <manifest>
        <attribute name="Main-Class" value="${jar.main.class}" />
        <attribute name="Class-Path" value="${jar.classpath}" />
    </manifest>
</jar>

For a more detailed answer on how to do this see:

Execute Java programs in a consistent environment

Community
  • 1
  • 1
Mark O'Connor
  • 76,015
  • 10
  • 139
  • 185
2

try with this:

<target name="jar" depends="clean,compile" >
   <jar destfile="./build/jar/DependencyFinder.jar">
    <fileset dir="./lib" includes="jbl.jar,mysql*.jar" />
    <fileset dir="./build/classes" excludes="**/form/*.class,**/orm/*.class,**/org/w3/xmldsig/*.class"/>
   </jar>
</target>
RaZieRSarE
  • 89
  • 4