Ant:NoClassDefFound异常

时间:2013-10-02 21:41:27

标签: java apache ant build.xml

我正在开发一个java项目。我的导师希望我们严格使用apache ant进行编译。我一无所知,也不知道阿帕奇蚂蚁。我已经生成了一个ant buildfile并对目标“run”进行了编辑,因此该程序将在ant run命令之后运行。

我收到了以下错误,

Exception in thread "main" java.lang.NoClassDefFoundError: src/Client
     [java] Caused by: java.lang.ClassNotFoundException: src.Client
     [java]     at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
     [java]     at java.security.AccessController.doPrivileged(Native Method)
     [java]     at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:321)
     [java]     at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
     [java]     at java.lang.ClassLoader.loadClass(ClassLoader.java:266)
     [java] Could not find the main class: src.Client. Program will exit.

我想我已经解决了所有目标依赖项。但是这个错误仍然存​​在。

有人能帮助我吗?这是我的ant build.xml

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- WARNING: Eclipse auto-generated file.
              Any modifications will be overwritten.
              To include a user specific buildfile here, simply create one in the same
              directory with the processing instruction <?eclipse.ant.import?>
              as the first entry and export the buildfile again. -->
<project basedir="." default="build" name="client">
    <property environment="env"/>
    <property name="ECLIPSE_HOME" value="../../../../usr/lib/eclipse"/>
    <property name="debuglevel" value="source,lines,vars"/>
    <property name="target" value="1.6"/>
    <property name="source" value="1.6"/>
    <path id="client.classpath">
        <pathelement location="bin"/>
        <pathelement location="libthrift-1.0.0-javadoc.jar"/>
        <pathelement location="libthrift-1.0.0.jar"/>
        <pathelement location="log4j-1.2.14.jar"/>
        <pathelement location="slf4j-api-1.5.8.jar"/>
        <pathelement location="slf4j-log4j12-1.5.8.jar"/>
    </path>
    <target name="init">
        <mkdir dir="bin"/>
    <mkdir dir="build"/>
        <copy includeemptydirs="false" todir="bin">
            <fileset dir="src">
                <exclude name="**/*.java"/>
            </fileset>
        </copy>
    </target>
    <target name="clean">
        <delete dir="bin"/>
    </target>
    <target depends="clean" name="cleanall"/>
    <target depends="build-subprojects,build-project" name="build"/>
    <target name="build-subprojects"/>
    <target depends="init" name="build-project">
        <echo message="${ant.project.name}: ${ant.file}"/>
        <javac debug="true" debuglevel="${debuglevel}" destdir="bin" source="${source}" target="${target}">
            <src path="src"/>
            <classpath refid="client.classpath"/>
        </javac>
    </target>
    <target name="jar" depends="init">
        <mkdir dir="build/jar" />
        <jar destfile="client.jar" basedir="bin">
            <manifest>
                <attribute name="Main-Class" value="src.Client" />
            </manifest>
        </jar>
    </target>
    <target description="Build all projects which reference this project. Useful to propagate changes." name="build-refprojects"/>
    <target description="copy Eclipse compiler jars to ant lib directory" name="init-eclipse-compiler">
        <copy todir="${ant.library.dir}">
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </copy>
        <unzip dest="${ant.library.dir}">
            <patternset includes="jdtCompilerAdapter.jar"/>
            <fileset dir="${ECLIPSE_HOME}/plugins" includes="org.eclipse.jdt.core_*.jar"/>
        </unzip>
    </target>
    <target description="compile project with Eclipse compiler" name="build-eclipse-compiler">
        <property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"/>
        <antcall target="build"/>
    </target>
    <target name="Client">
        <java classname="Client" failonerror="true" fork="yes">
            <classpath refid="client.classpath"/>
        </java>
    </target>
    <target name="run" depends="jar">
        <java jar="client.jar" fork="true">
        </java>
    </target>
</project>

有人可以为我发现错误吗?

1 个答案:

答案 0 :(得分:2)

Exception java.lang.NoClassDefFoundError发生在您的代码所依赖的某个类,您的或库中时,尝试使用尚未加载的符号。类加载器使用java路径package_aa.package_bb.SomeClass作为它知道的一组类的键,由-classpath或-cp指定。在您的情况下,这个“存储库是bin, libthrift-1.0.0-javadoc.jar, libthrift-1.0.0.jar, log4j-1.2.14.jar, slf4j-api-1.5.8.jar, slf4j-log4j12-1.5.8.jar

在你的上下文中,当要加载类p1.p2.Client时,类加载器会查找bin / p1 / p2 / Client.class,在每个jar中查找/p1/p2/Client.class以及何时所有搜索路径都已耗尽,抛出异常。

在build.xml的这一部分:

<target name="jar" depends="init">
    <mkdir dir="build/jar" />
    <jar destfile="client.jar" basedir="bin">
        <manifest>
            <attribute name="Main-Class" value="src.Client" />
        </manifest>
    </jar>
</target>

value="src.Client"是错误的,它应该是逻辑java路径,即包+类名。 src是一个目录而不是包。如果你没有任何包,它只是classname。

我认为

<attribute name="Main-Class" value="Client" />

这应该可以解决问题。

参考:

首次调试后,

编辑,出现第二个错误:

<target name="run" depends="jar">
    <java jar="client.jar" fork="true">
    </java>
</target>

因为Client.jar不是自治的,所以无法工作,它需要大量的depndencies,这里没有指定。您应该使用在build.xml之上定义的类路径ref。

可能写成:

<target name="run" depends="jar">
    <java classpathref="client.classpath" fork="true" classname="Client" />
</target>