aspose file tools*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes (NX)path of db file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "(NX)path of db file" Watch "(NX)path of db file" New topic
Author

(NX)path of db file

biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
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.


biang
Ta Ri Ki Sun
Ranch Hand

Joined: Mar 26, 2002
Posts: 442
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

Joined: Jun 02, 2003
Posts: 91
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

Joined: Mar 28, 2003
Posts: 11477
    
  94

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


The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24x7 Personal blog
biang lin
Ranch Hand

Joined: Jun 02, 2003
Posts: 91
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

Joined: Mar 28, 2003
Posts: 11477
    
  94

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

Joined: Jun 02, 2003
Posts: 91
Hi Andrew,
I got it.Thank you!!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: (NX)path of db file