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

Arnold said
Thank you very mutch. I was looking for this for a long time. Think that it was so simple
Chears
/Arnold
Shane O'Sullivan said
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.
Shane
Chris said
I had the same problem and this came up in google, thanks for the tip Shane!
Chears
/Chris
Mayur Naik said
Thanks for taking the time to write down this useful tip … I too came across it by googling.
Samir Savla said
Thanks for this…I googled too and saw this info and got the solution to a problem i was stuck on
Shane O'Sullivan said
Chris, Samir and Mayur,
You’re all very welcome
Shane
Steve Claflin said
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=.
Eugene said
Oh Dear, thanks a lot. %Tears of happiness%
shobana said
Hi thank u so much for ur help
noboundaries said
Thanks much! It works for me