Setting the working directory for an Ant Java task

Just a tiny Ant tip – I’ve been fooling around with Apache Ant for the first time, and needed to change the working directory so that, rather than it being the folder in which the build.xml file is located, it be somewhere else. This enables files in other directories (which I don’t control) to use relative paths to other files. I tried setting the System’s “user.dir” and “basedir” properties, but these didn’t work.

So, the solution is very simple, in your <java> task, set “fork” to true, and then set the “dir” property to the folder you want to use as a working directory.

e.g.

<target name=”doMyBidding”>

<java fork=”true” dir=”c:\work\lib”>

……..

</java>

</target>

No doubt this has been documented somewhere else, but a quick search on Google didn’t readily find it for me, so here you go. Enjoy!

11 thoughts on “Setting the working directory for an Ant Java task

  1. Thanks for this…I googled too and saw this info and got the solution to a problem i was stuck on 🙂

  2. I was looking to use the current directory as my working directory, with the build file located in a fixed location. It turns out that a command line option -Dbasedir=. will set the working directory, without needing to then supply the dir attribute for the java task. As in:

    ant -buildfile C:\whatever\build.xml -Dbasedir=.

  3. Now all interest is house simply because each businessman Allow us look at the expense. So I get this whisper, “can Jinn be killed?” one day. You should be willing to adjust to altering circumstances.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s