How to include MongoDB jar in ANT script ? What kind of tags should I use to include the jar?

575 views Asked by At

Whenever I try to run my code through ANT Script , I get this error " error: package com.mongodb does not exist" .When I run my code in Eclipse it executes successfully and when I execute my code through ANT Script I get this error . I have included the mongoDB jar in the project .

Error -" package com.mongodb does not exist "

build.xml -

<!-- Sets variables which can later be used. -->
<!-- The value of a property is accessed via ${} -->
<property name="src.dir" location="src" />
<property name="build.dir" location="bin" />
<property name="dist.dir" location="dist" />
<property name="docs.dir" location="docs" />

<!-- Deletes the existing build, docs and dist directory -->
<target name="clean">
    <delete dir="${build.dir}" />
    <delete dir="${docs.dir}" />
    <delete dir="${dist.dir}" />
</target>

<!-- Creates the build, docs and dist directory -->
<target name="makedir">
    <mkdir dir="${build.dir}" />
    <mkdir dir="${docs.dir}" />
    <mkdir dir="${dist.dir}" />
</target>

<path id="master-classpath">
    <pathelement path="./lib/mongo-2.10.1.jar" >
        </pathelement>
</path>


<!-- Compiles the java code (including the usage of library for JUnit -->
<target name="compile" depends="clean, makedir">
    <javac srcdir="${src.dir}" destdir="${build.dir}" includeantruntime="false">
        <classpath refid="master-classpath" />
    </javac>

</target>

<!-- Creates Javadoc -->
<target name="docs" depends="compile">
    <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
        <!-- Define which files / directory should get included, we include all -->
        <fileset dir="${src.dir}">
            <include name="**" />
        </fileset>
    </javadoc>
</target>

<!--Creates the deployable jar file -->
<target name="jar" depends="compile">
    <jar destfile="${dist.dir}\CsvReaderExample.jar" basedir="${build.dir}">
        <manifest>
            <attribute name="Main-Class" value="test.Main" />
        </manifest>
    </jar>
</target>

<target name="main" depends="compile, jar, docs">
    <echo>Hello Ankur - Welcome to Apache Ant!</echo>
    <description>Main target</description>
</target>

1

There are 1 answers

2
Amaresh Pattanayak On BEST ANSWER

just add the class path reference in ant script.You can see a tag called

  <classpath>

inside this tag you can find another tag,

<pathelement path="./lib/rt.jar" />

add another pathelement tag and put the mongodb jar name. Like:-<pathelement path="./lib/com.mongodb.jar" />

And make sure that the respective jar should be present inside your lib folder also. I think this will work fine.Try it and let me know.