我正在尝试关注this Ant guide ,并且我在尝试添加 log4j 外部库的部分中遇到错误。按照指南的说明,我已经下载了相应的this log4j jar并将其放入我的 lib 库中。我还尝试将 jar 文件直接解压到 lib 文件夹中。
错误是,虽然我已经导入了必要的库并将Ant指向了我的build.xml中,但它无法找到它。
error: package org.apache.log4j does not exist ...
error: could not find symbol BasicConfigurator.configure(); ...
^
这是我的 build.xml 文件,它几乎是从示例中复制并粘贴的(减去“oata”包名称:
<property name="src.dir" value="src"/>
<property name="build.dir" value="build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jar"/>
<property name="main-class" value="HelloWorld"/>
<property name="lib.dir" value="lib"/>
<target name="clean">
<delete dir="${build.dir}"/>
</target>
<path id="classpath">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="classpath"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}"/>
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" classpathref="classpath">
<manifest>
<attribute name="Main-Class" value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java fork="true" classname="${main-class}">
<classpath>
<path refid="classpath"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
最佳答案
I also tried just unpacking the jar file directly into the lib folder.
解压 JAR 是完全错误的。 永远不要这样做。
您的链接引用的文件是 ZIP 文件,而不是 JAR。它里面有log4j-1.2.16.jar。如果打开它,您将在 org.apache.log4j 包中看到 BasicConfigurator.class。
关于java - Ant 无法检测到外部库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8797830/