Brett McCann
Brett McCann

Reputation: 2519

error running java in another dir from ant

I have a simple class, FabricClient, in the package client, with a public static void main(). This package is in the 'bin' directory of my project. From the bin directory, on the command line, I can execute the main in this class as follows:

user@sys /c/projects/myproject/bin
$ java -classpath .;c:\dir\my.jar client.FabricClient

I want to execute my ant build.xml file from the parent directory (/c/projects/myproject) from where I run the java command by hand. I've tried using the dir attribute as follows:

  <target name="run-client">
    <java classname="client.FabricClient"
        classpath="${classpath}"
        fork="true"
        failonerror="true"
        dir="./bin">
     </java>          
  </target>

My classpath variable does have the current directory as well as the directory of my.jar as used on the command line (my ant command to compile the class works fine with the same variable)

Here are the settings used to generate the classpath

  <property name="my.jar" location="c:\dir\my.jar" />
  <property name="classpath" location=".:${my.jar}:." />

I'm getting the typical java.lang.NoClassDefFound Error: client/FabricClient

I'm using Ant version 1.7.0

If I move the build.xml file into bin and remove the dir attribute, it works fine. Can you run java from a directory other than the local directory? I thought this was what the dir attribute did.

Upvotes: 0

Views: 404

Answers (2)

Riccardo Cossu
Riccardo Cossu

Reputation: 2739

Paths in ant are usually referred like this:

<java classname="client.FabricClient"
    fork="true"
    failonerror="true"
    dir="./bin">
    <classpath>
         <pathelement location="C:/dir/my.jar"/>        
    </classpath>

it is more readable and reliable

Upvotes: 1

Erik
Erik

Reputation: 91260

Your classpath should contain the file my.jar, not the directory where my.jar is.

Upvotes: 0

Related Questions