Shane O'Sullivan's technical blog… really ties the room together

Setting the working directory for an Ant Java task

Posted by Shane O'Sullivan on January 3, 2007

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.


<target name=”doMyBidding”>

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




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!

10 Responses to “Setting the working directory for an Ant Java task”

  1. Arnold said

    Thank you very mutch. I was looking for this for a long time. Think that it was so simple 🙂

  2. Hi Arnold,

    You’re very welcome – I think a lot of people could be struggling with this. It doesn’t seem to appear anywhere when I google it.


  3. Chris said

    I had the same problem and this came up in google, thanks for the tip Shane!


  4. Mayur Naik said

    Thanks for taking the time to write down this useful tip … I too came across it by googling.

  5. Samir Savla said

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

  6. Chris, Samir and Mayur,

    You’re all very welcome 🙂


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

  8. Eugene said

    Oh Dear, thanks a lot. %Tears of happiness%

  9. shobana said

    Hi thank u so much for ur help

  10. noboundaries said

    Thanks much! It works for me

Leave a Reply

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

You are commenting using your 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

%d bloggers like this: