Home arrow static arrow Java Programming [Archive] - How to change current working directory?
Warning: Creating default object from empty value in /www/htdocs/w008deb8/wiki/components/com_staticxt/staticxt.php on line 51
Java Programming [Archive] - How to change current working directory?
This topic has 3 replies on 1 page.

Posts:3,369
Registered: 24.10.97
How to change current working directory?  
Jul 29, 2003 2:23 AM



 
How can I change the current working directory within a Java application? So that when reading a file (without full) path is found that exists in this other directory that i want to make the current one?

My Java application needs to start in a specific directory as there are some other files that are refered to from the app in this directory (without full directory path). during the processing (that the app does), some XML files (in a different directory) are loaded/processed and some of them have references to DTD files without a full directory path. these referenced files are in the same directory as the XML files, but the XML processor (Apache Xalan/Xerces) trys to read them from the current wirking directory. :-(
 

Posts:3,369
Registered: 24.10.97
Re: How to change current working directory?  
Jul 29, 2003 2:33 AM (reply 1 of 3)



 
System.setProperty("user.dir", sourceDir);

did the trick. :-]
 

Posts:90
Registered: 7/29/97
Re: How to change current working directory?  
Jul 8, 2004 2:17 PM (reply 2 of 3)



 
No. Actually it does only half work. If you use new File("myfile").exists() after setting new working directory it will give you false, although the file exists. However if you try File("myfile").getAbsoluteFile().exists(), then you will get true. Bad thing that this work aroud doesn't work if you start some other app under, which doesn't use the trick to find files. Any other idea? I'm too lazy to see in implementation of FileSystem should givving the answer. Anybody help?
 

Posts:1,657
Registered: 1/8/98
Re: How to change current working directory?  
Jul 8, 2004 7:56 PM (reply 3 of 3)



 
Don't try to change the "user.dir" - keep a reference to the "currentDir" in your application and always refer to that to construct a filename/path. What's probably confusing you is that the Windows SDK does this for you when you "setCurrentDir" or whatever it's called. And since all file operations go through the same SDK, it behaves as if the application "moved" to this other directory.
 
This topic has 3 replies on 1 page.