4

I see the question "what user.dir means?", how is it identified, why it does not work properly, how to change it. But I do not see the basic question: why do we need "user.dir" in the first place, after all operating systems support "." for current directory?

I see that user.dir = absolute path to the current directory whareas . is relative. Is it just a shorthand for File(".").getAbsoluteFile().getParentFile().getName()?

Community
  • 1
  • 1
Val
  • 1
  • 8
  • 40
  • 64

2 Answers2

2

Because the current directory can change, based on a program navigating through the filesystem. The directory the program started from cannot change.

Thorn G
  • 12,620
  • 2
  • 44
  • 56
  • Do I navigate when open a File("abc")? What is navigating? – Val Oct 16 '13 at 13:53
  • 1
    Perhaps you are using the IO libraries or spawning a shell. The point is that a `.` reference in some filesystem path means "The current directory, as resolved by the filesystem." Consider the path `/usr/bin/.././.././bin/./` -- the `.` path segments do not necessarily mean the same thing as `user.dir`. – Thorn G Oct 16 '13 at 13:57
  • I do not get your point. I do not even uderstand if you have one point or two. What is the problem with . to be resoved by file system if it is always resolved as current directory? As of the second point, I see that `.` is very different from `/usr/bin/.././.././bin/./` and do not understand what you are solving with `user.dir`. It looks like you are saying that if I open a path that contains `.` in it this will break my `File(".")`. However, it won't. `File(".")` will always point to current dire, regardless my "navigations". – Val Oct 16 '13 at 14:02
  • 2
    IIRC, it is specifically stated somewhere in the Java specifications (though I do not remember where), that Java programs cannot changed their working directory. – Dolda2000 Oct 16 '13 at 14:34
  • @Dolda2000 That seems likely. My larger point though is that `.`'s meaning is *context-sensitive* depending on where you encounter it. It could mean different things *within the same path*. However `user.dir` is an *absolute* path. It always means "The directory the program launched from." – Thorn G Oct 16 '13 at 14:37
  • This is reduculous. File(".").getAbsolutePath() is also absolute. By saying that `.` is different in `whatever/./../bin` you say that letter `a` is context-sensetive and, therefore, we need a `System.getProperty("a")` -- the first letter of alphabet that is absolute and does not change while program navigates the text!! – Val Oct 16 '13 at 16:10
1

I think that the purpose of "user.dir" property is the same as pwd command in Linux or GetCurrentDirectory in Windows. I think it is more clear this way because we clearly use pwd when we want to get the (absolute) path of the current directory. In contrast, . is a shorthand for the current dir when you are not interested to know it but, rather, just tell the system that we want to use the current path. It is reasonable to use the shorthand in this case. First means current path relative to the root, second -- current path relative to the current path.

The difference may be important when you communicate between processes. If you pass a path to another process in your system, it may have a different current dir and, thus, you better prefer the absolute path. On the other hand, if you pass a path to another system, then relative path may be preferred.

Val
  • 1
  • 8
  • 40
  • 64