0

每当我尝试通过ANT 脚本运行我的代码时,我都会收到此错误“错误: com.mongodb 包不存在”。当我在Eclipse中运行我的代码时,它会成功执行,当我通过ANT脚本执行我的代码时,我会收到此错误. 我已经在项目中包含了 mongoDB jar。

错误 - “包 com.mongodb 不存在”

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>

4

1 回答 1

1

只需在 ant 脚本中添加类路径引用即可。您可以看到一个名为

  <classpath>

在这个标签内你可以找到另一个标签,

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

添加另一个 pathelement 标记并放置 mongodb jar 名称。喜欢:-<pathelement path="./lib/com.mongodb.jar" />

并确保相应的 jar 也应该存在于您的 lib 文件夹中。我认为这会很好用。试试吧,让我知道。

于 2015-01-15T09:21:36.323 回答