JGFMK
JGFMK

Reputation: 8904

Ant script giving class not found exception - why?

<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>

When I run the project gives java.lang.ClassNotFoundException: jgf.EMDR_Client

I echo the classpath for compile and run

compile is:

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

run is:

/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

My JAVA_HOME is : /Library/Java/Home

Is the ant java dir attribute borked?

enter image description here

enter image description here

Upvotes: 0

Views: 6385

Answers (1)

Udo Klimaschewski
Udo Klimaschewski

Reputation: 5325

You should use the classpath tag, though path should also work IMHO. But use it correctly. Do not add classes directly to the path, but only jars or directories:

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

That should give you a path like:

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

Java will then look ito the jars first, then into the directory /Users/JGF/Projects/emdr/build/classes.

To find the class jgf.EMDR_Client, Java will look for a file named EMDR_Client.class in a sub-directory named jgf. So it now should find your class.

Again: Classpath elements are not the class files, but directories or JAR files (which are compressed directories)

Upvotes: 3

Related Questions