• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to use slash to access base directory in Java?  RSS feed

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
Firefox Browser Java Mac OS X
  • Mark post as helpful
  • send pies
  • 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

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!