• 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

How to use slash to access base directory in Java?

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have to write entire path, when my code has to access a particular file, lying in my project's home directory. How can I just use slash to reach home directory?

Example:
System.setProperty("webdriver.chrome.driver","D:/dumps/automation/Pulled/gitproject/chromedriverWin.exe");

but I want to use

System.setProperty("webdriver.chrome.driver","/chromedriverWin.exe");

 
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

Try System.getProperty("user.dir") to represents the D:\.... up to the last file separator. You should use the File.separator in your path.
 
Paul Jimenezzz
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually , why I wanted this is because of two reasons:
1. I have to write long file path
2. I would not able to run this code on LINUX or UNIX machine, because they dont have drives(C:, D:).
So using your idea I am sure I can achieve #1 but #2 would remain a problem. I wanted a way where slash can redirect to parent directory.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If the paths are going to be different on various systems, they should likely not be hard-coded, but stored and retrieved from properties file.
 
K. Tsang
Bartender
Posts: 3648
16
Android Mac OS X Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I'm not mistaken on Windows the "/" alone represents the root too (eg C:) if your command prompt is running in C:

So if you used your previous posted code. The program will throw FileNotFoundException because it's looking for C:\filename.exe or D:\filename.exe

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic