2013-10-02 115 views
2

我正在開發一個java項目。我的老師希望我們嚴格使用apache ant進行編譯。我對此一無所知,也不瞭解apache ant。我已經生成了一個ant buildfile,並對目標「run」進行了編輯,以便程序在ant run命令之後運行。Ant:NoClassDef發現異常

我收到以下錯誤,

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. 

我想我已經解決了所有的目標相關性。但是這個錯誤仍然存​​在。

有人可以幫我嗎?這是我的螞蟻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> 

任何人都可以發現我的錯誤嗎?

+0

在client.classpath添加SRC路徑也。並嘗試。 – Zeus

回答

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,尋找/ 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是一個不是包的目錄如果你沒有任何包,它只是類名

我相信

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

這應該做的伎倆。

參考:

編輯,出現的第二個錯誤:

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

不行,因爲client.jar的不是自主的,它需要大量的depndencies,其中的AREN這裏沒有指定。你應該使用定義在build.xml之上的classpath ref。

可能這樣寫道:

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

嗯....它不工作.... –

+0

什麼是錯誤信息? – Aubin

+0

它是一樣的... –