• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

(NX)path of db file

 
biang lin
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a question of how to write the db file path:
db file path: "d:/URLyBird/db.db"
class file path: "d:/URLyBird/classes/suncertify/main.class"
How can I access the db file in main.class??
My code: this.getClass().getResource("..//..//db.db");
But it didn't work.
And what if the class is in jar file? The db file must in the same path of the jar file.
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by biang lin:
Hi,
I have a question of how to write the db file path:
db file path: "d:/URLyBird/db.db"
class file path: "d:/URLyBird/classes/suncertify/main.class"
How can I access the db file in main.class??
My code: this.getClass().getResource("..//..//db.db");
But it didn't work.
And what if the class is in jar file? The db file must in the same path of the jar file.

you have to determine the current working directory first, using System.getProperty("user.dir"); , then append that to the relative path configured in the database, thats if a relative path is used, else just return the absolute path, or force the user to configure relative path only.
so basically its like this
the getProperty("user.dir") when java command is invoked from your "d:/URLyBird" will return exactly that.
now if the user configured the path to DB as \\db.db or /db.db then append that to your current directory and you'll have d:/URLyBird/db.db, but if the configured path does not start with File.separator then just return the path without the current directory appended.
theres no need to get resource, your File constructor will take the String
 
biang lin
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ta Ri Ki Sun ,
Thanks for your reply.
But what if user start the program at "C:/temp"? In this case the current working directory is "C:/temp" ,and I can not append the relative path.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11853
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Biang,
Have you considered using a JFileChooser so that the user can browse to the location of the data file?
Or looking at your class path to see if you can work out where your executable jar file is - the database may be with the executable jar file. (Note: try building an executable jar file, and look at what the class path is from within that - you should then see what I mean. Trying to use the classpath from your standalone classes is not as easy)
Regards, Andrew
 
biang lin
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
How can I "look at what the class path is from within that "?
I am still confused.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 11853
185
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Biang,
I recomend you look at the JFileChooser as your first option.
But if you then decide you still want to try and find the file programatically, you could look at the class path using 'System.getProperty("java.class.path")'.
Regards, Andrew
 
biang lin
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Andrew,
I got it.Thank you!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic