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>

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

JGFMK
  • 8,425
  • 4
  • 58
  • 92

1 Answers1

3

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)

Udo Klimaschewski
  • 5,150
  • 1
  • 28
  • 41
  • I modified script and took out reference to classpath.run and just directly referenced classpath.lib. Still called no joy... Was not using classpath tag - rather the classpathref attribute of the java task though still.. Still no joy. – JGFMK Dec 17 '12 at 13:45
  • Modified java task and embeddd classpath tag instead of attribute too. That didn't work either. Same old class not found exception. – JGFMK Dec 17 '12 at 13:49
  • I will add that the zmq.jar is an odd beast - part of the ZeroMQ project - I believe it is just a JNI wrapper around native classes that I use to run on my Mac. That couldn't be some bizarre reason for the class not being found could it? - It's in the build/classes directory as you can see from screenshot – JGFMK Dec 17 '12 at 13:52
  • added some clarification to my answer – Udo Klimaschewski Dec 17 '12 at 13:57
  • right - that makes some sense - will give that a try.. But it sort of makes me think what is point of dir attribute on java task in Ant. – JGFMK Dec 17 '12 at 14:11
  • That got me past that hurdle. Now I have Exception in thread "main" java.lang.UnsatisfiedLinkError: no jzmq in java.library.path – JGFMK Dec 17 '12 at 14:15
  • This was post I used to create the jar ... http://technicalvvkydv.blogspot.co.uk/2011/06/making-zeromq-jar-for-mac.html – JGFMK Dec 17 '12 at 14:16
  • The `` task can be used to create a list of files, starting at directory specified with the `dir` attribute. This `` task is not what you need here, as you do not want to add individual class files to your path, but instead the directory holding the package/class structure. As this is what Java uses to load classes. – Udo Klimaschewski Dec 17 '12 at 14:17
  • This may shed some light on the unsatisfied link error - https://github.com/zeromq/jzmq/issues/29 – JGFMK Dec 17 '12 at 14:20
  • Please open a new question for this, as it is a different topic. Do not forget to accept answers, if they answered your initial question correctly. – Udo Klimaschewski Dec 17 '12 at 14:23