Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

idiom to get the current directory in java7?

 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to write a line of code to get the absolute path of the working/current directory. The best I could come up with was this ugly piece of code. How would you make it better? Is there a common idiom?
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeanne,

Is this what you mean, or is this newbie out of his depth?



cheers,
Daniel
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Doesn't System.getProperty("user.dir") do what you want?

I found an old class I wrote two years ago:-That appears to print the absolute path for my current directory.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why yes, it does. I forgot about that!

Daniel: I gave you a cow for being first with the answer. And for not worrying about pointing out a moderator was missing something obvious!
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also:

or
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote: . . . Daniel: I gave you a cow for being first with the answer. . . .
And he beat me by two minutes
 
Daniel Hirning
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
Daniel: I gave you a cow for being first with the answer. And for not worrying about pointing out a moderator was missing something obvious!


Wow Jeanne, thanks for the cow!
This would be a case of you having forgotten more than I know.

Campbell Ritchie wrote:
And he beat me by two minutes


I would share the cow but it would make a mess of the Saloon
 
Jelle Klap
Bartender
Posts: 1951
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The thing to remember with the user.dir system variable is that it represents the directory from where the JVM was started, which can be anywhere on the filesystem, and it may or may not be what you're after. You definitely can't depend on it to find out the root of the application.
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Hirning wrote: . . . I would share the cow but it would make a mess of the Saloon
That doesn't bear thinking about
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34095
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Also:

or

Thanks Mike. I didn't realize you could call Paths.get("") with an empty string. That's good to know!
 
Palak Mathur
Ranch Hand
Posts: 328
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Also:

or


This is really nice.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic