Home > Java > Changing the Current Directory

Changing the Current Directory

February 5th, 2009 jlee
This entry is part 3 of 5 in the series Tips

One of the more common requests I see online from beginners (and from a not-so-beginner just now) is how to change the current directory.  This one is really simple, so here’s a quick snippet and the output.

System.out.println(new File(“.”).getAbsolutePath());
System.setProperty(“user.dir”, System.getProperty(“java.io.tmpdir”));
System.out.println(new File(“.”).getAbsolutePath());

And the output:

/Users/jlee/.
/tmp/.

See?  Simple.

Share and Enjoy:
  • Digg
  • Reddit
  • del.icio.us
  • Google Bookmarks
  • DZone
  • LinkedIn
  • Technorati
  • email

Technorati Tags: , ,

Categories: Java
  1. February 5th, 2009 at 18:21 | #1

    Does this change pwd, or just how File’s string constructors build FS paths? I can see this producing weird and confusing results if you have native code that handles paths that’s not aware of the System property.

  2. February 5th, 2009 at 18:23 | #2

    I’m not sure how it’d affect JNI. People who use JNI deserve whatever pain they get. :)

Comments are closed.