wood burning stoves 2.0*
The moose likes Features new in Java 7 and the fly likes idiom to get the current directory in java7? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Features new in Java 7
Bookmark "idiom to get the current directory in java7?" Watch "idiom to get the current directory in java7?" New topic
Author

idiom to get the current directory in java7?

Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30780
    
157

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?


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Daniel Hirning
Ranch Hand

Joined: Sep 16, 2013
Posts: 50
    
    1

Hi Jeanne,

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



cheers,
Daniel
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: May 26, 2003
Posts: 30780
    
157

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

Joined: Mar 05, 2008
Posts: 3018
    
  10
Also:

or
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: Sep 16, 2013
Posts: 50
    
    1

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

Joined: Mar 10, 2008
Posts: 1779
    
    7

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.


Build a man a fire, and he'll be warm for a day. Set a man on fire, and he'll be warm for the rest of his life.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
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

Joined: May 26, 2003
Posts: 30780
    
157

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

Joined: Jan 29, 2007
Posts: 313

Mike Simmons wrote:Also:

or


This is really nice.


Palak Mathur | My Blog | TechJaunt | What is JavaRanch? | List of All FAQs
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: idiom to get the current directory in java7?