CWD : Java Glossary

CWD
CWD (Current Working Directory). In Windows, it is the directory that prints when you type CD (Change Directory) on the command line. It is the default directory for the File class if you don’t specify a fully qualified filename. In Java, it is the current directory at the time java.exe was invoked. You can’t change it on the fly, but you can determine what it is with:
// Getting the CWD: Current Wording Directory
// namely the current directory when java.exe was invoked.
// It cannot be changed on the fly.
final String cwd = new File( "." ).getCanonicalPath();

// Which may or may not be the same as:
final String userDir = System.getProperty( "user.dir" );
You can of course spawn a script to a command shell that would be able change the CWD inside itself, but not for the spawning Java app.

You can fake a CWD by maintaining your own CWD variable and expanding all relative filenames to absolute yourself.

The catch is, a Java program cannot dynamically change the CWD. The closest it could to that is spawn command scripts with embedded CD commands, that change on the local CWD within the script. It is odd that code implementing scripts can change the CWD, but not Java. I gather Java’s designers figured that you could not could on every possible OS (Operating System) permitting you to change the CWD. They could not thus safely built it in as a WORA (Write Once, Run Anywhere) feature.


CMP homejump to top

available on the web at:

http://mindprod.com/jgloss/cwd.html
  

optional Replicator mirror
of mindprod.com
on local hard disk J:

J:\mindprod\jgloss\cwd.html
logo
Please email your , letters to the editor, errors, omissions, typos, formatting errors, ambiguities, unclear wording, broken/redirected link reports, suggestions to improve this page or comments to Roedy Green : feedback email. If you want your message, your name or email kept confidential, not considered for public posting, please explicitly specify that. Unless you state otherwise, I will treat your message as a letter to the editor that I may or may not publish in the feedback section. After that, it will be too late to retract it. If you disagree with something I said, especially when sending an ad-hominem attack, a rant composed mainly of obscenities or a death threat, please quote the offending passage and cite the web page where you found it, tell me why you think it is wrong, and, if possible, provide some supporting evidence. I can’t very well fix erroneous or ambiguous text if I can’t find it.
Blog
IP:[65.110.21.43]
Your face IP:[54.224.79.93]
You are visitor number 14,541.