2012-12-17 38 views
0
<project name="MyProject" default="run" basedir="."> 
    <description> 
     simple example build file 
    </description> 
    <!-- set global properties for this build --> 
    <property name="src" location="./src"/> 
    <property name="build" location="./build"/> 
    <property name="lib" location="./lib"/> 
    <property name="zmq.dir" location="/usr/local/share/java"/> 

    <path id="classpath.compile"> 
     <fileset dir="${lib}"> 
      <include name="**/*.jar"/> 
     </fileset> 
     <pathelement location="${zmq.dir}/zmq.jar"/> 
    </path> 

    <path id="classpath.run"> 
    <path refid="classpath.compile"/> 
     <fileset dir="${build}/classes"> 
      <include name="**/*.class"/> 
     </fileset> 
    </path> 

    <target name="clean" 
     description="clean up" > 
    <delete dir="${build}"/> 
    </target> 

    <target name="init" depends="clean"> 
    <!-- Create the build directory structure used by compile --> 
    <mkdir dir="${build}"/> 
    <mkdir dir="${build}/classes"/> 
    </target> 

    <target name="compile" depends="init" 
     description="compile the source " > 
    <property name="compile.classpath" refid="classpath.compile"/> 
    <echo message="Compiling with classpath ${compile.classpath}"/> 
    <javac srcdir="${src}" 
      destdir="${build}/classes"> 
     <classpath refid="classpath.compile"/> 
    </javac> 
    </target> 

    <target name="run" depends="compile"> 
    <property name="run.classpath" refid="classpath.run"/> 
    <echo message="Running with classpath ${run.classpath}"/> 
    <java fork="true" 
      dir="${build}/classes" 
      classname="jgf.EMDR_Client" 
      classpathref="classpath.run"/> 
    </target> 
</project> 

當我運行該項目給java.lang.ClassNotFoundException: jgf.EMDR_ClientAnt腳本給類沒有發現異常 - 爲什麼?

我附和類路徑編譯和運行

編譯是:

/Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar 

運行是:

/Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar:/Users/JGF/Projects/emdr/build/classes/jgf/EMDR_Client.class 

我JAVA_HOME是:/Library/Java/Home

ant java dir attribute borked?

enter image description here

enter image description here

回答

3

您應該使用的classpath標籤,雖然路徑也應努力恕我直言。但正確使用它。 不要直接添加類路徑,而只罐子或目錄:

<path id="classpath.run"> 
    <path refid="classpath.compile"/> 
    <pathelement location="${build}/classes" /> 
</path> 

這應該給你喜歡的路徑:

/Users/JGF/Projects/emdr/lib/json_simple-1.1.jar:/usr/local/share/java/zmq.jar:/Users/JGF/Projects/emdr/build/classes 

那麼Java會看ITO的罐子,然後再進入目錄/Users/JGF/Projects/emdr/build/classes

要查找類jgf.EMDR_Client,Java將在名爲jgf的子目錄中查找名爲EMDR_Client.class的文件。所以現在應該找到你的班級。

還是那句話:類路徑元素不是類文件,但目錄或JAR文件(被壓縮的目錄)

+0

我修改劇本,拿出參考classpath.run,只是直接引用classpath.lib。仍然沒有歡樂......沒有使用類路徑標記 - 而是java任務的classpathref屬性,儘管如此...仍然沒有喜悅。 – JGFMK

+0

已修改的java任務和embeddd類路徑標記而不是屬性。那也行不通。相同的舊類沒有發現異常。 – JGFMK

+0

我會補充一點,zmq.jar是一個奇怪的野獸 - ZeroMQ項目的一部分 - 我相信它只是一個JNI包裝本地類,我用它在我的Mac上運行。這不可能是一個奇怪的原因,這個班不被發現嗎? - 它在build/classes目錄中,你可以從屏幕截圖 – JGFMK