Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

idiom to get the current directory in java7?

 
author & internet detective
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 50
1
Android Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jeanne,

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



cheers,
Daniel
 
Marshal
Posts: 79012
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Master Rancher
Posts: 4756
71
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also:

or
 
Campbell Ritchie
Marshal
Posts: 79012
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 1952
7
Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79012
375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 41849
904
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 343
Mac OS X Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:Also:

or



This is really nice.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic