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_Client

我回显类路径以进行编译和运行

编译是:

/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 属性是不是很糟糕?

在此处输入图像描述

在此处输入图像描述

4

1 回答 1

3

您应该使用类路径标签,尽管路径也应该适用于恕我直言。但要正确使用。不要将类直接添加到路径中,而只添加 jar 或目录:

<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 将首先查看 jars,然后查看目录/Users/JGF/Projects/emdr/build/classes

为了找到类jgf.EMDR_Client,Java 将在名为EMDR_Client.class的子目录中查找名为 的文件jgf。所以它现在应该找到你的类。

同样:Classpath 元素不是类文件,而是目录或 JAR 文件(它们是压缩目录)

于 2012-12-17T13:34:27.587 回答