每当我尝试通过ANT脚本运行我的代码时,我都会收到此错误"错误:包com.mongodb不存在"。当我在 Eclipse 中运行我的代码时,它会成功执行,当我通过 ANT 脚本执行我的代码时,我收到此错误。我已经在项目中包含了mongoDB jar。
错误 -" 包 com.mongodb 不存在"
构建.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>
只需在 ant 脚本中添加类路径引用。您可以看到一个名为
<classpath>
在此标签中,您可以找到另一个标签,
<pathelement path="./lib/rt.jar" />
添加另一个路径元素标签并放置 MongoDB Jar 名称。喜欢:- <pathelement path="./lib/com.mongodb.jar" />
并确保相应的 jar 也应该存在于您的 lib 文件夹中。我认为这将正常工作。试试吧,让我知道。