File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Path to store Embedded Database files Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Path to store Embedded Database files" Watch "Path to store Embedded Database files" New topic

Path to store Embedded Database files

Darshit Hirani

Joined: Oct 03, 2005
Posts: 5
I am making an application with embedded database. This application will be Swing based and it will be a single jar file. So my aim is that everything should happen in the jar file.

Since I am using embedded database i want all my databse files ot be in the jar files. I am creating a folder/ package say "database" in "src" folder which will be home to all database related files.

NOw the problem is how do i define the path dynamically. Like consider the following case.

While i am creating the project the "database" folder will be in "home/project_name/src/database". But in the jar file this folder will be at "place_where_jar_file_is_located/database".

I used System.getProperties("user.dir") to get current working directory and i can go into src/database through it. But then in the jar the structure is different. SO the same won't work

I am getting confused in handling/defining the path.

How should I be handling it???

Edwin Dalorzo
Ranch Hand

Joined: Dec 31, 2004
Posts: 961
First of all you must consider what will happen when you add records to your database if your database file is stored within your application jar.

I would recommend that your database file should be located somewhere else, but in the application jar. As a matter of fact, (and somebody please correct me if I am wrong), I do not believe you can modified the contents of a file stored in a zip format, like a jar. Now, even though your file is read-only I would consider to put it somewhere else.

That being said, there are plenty of options to solve this problem. The first is to put the file in the same directory of your jar.

Then simply:


Another option is to pass the path to the directory of the database file in the java application launcher classpath:

java -cp myjar.jar; /home/obiwan/database/

Then simply:

InputStream in = ClassLoader.getSystemResourceAsStream("data.db");

Or you could pass the path to your file at the application start up

java -jar myJar.jar /home/obiwan/database/jedis.db

Or you could save the path to your file in a properties file and get it from it, or you ask in the GUI the path to the file (like in MS Access database).

Do any of these sound useful?
[ June 06, 2006: Message edited by: Edwin Dalorzo ]
Darshit Hirani

Joined: Oct 03, 2005
Posts: 5
Thanks Edwin.

AS a part of the application I need everything into one file so was wondering if it is possible.

Thanks for the other ways, if there is no way to put everything into one i will use one of the methods you mentioned.
I agree. Here's the link:
subject: Path to store Embedded Database files
It's not a secret anymore!