2

Although I have done much research I am still struggling to find the correct place to put my access database file so that it can be used with my java project and my final jar file (without having to change the code for each case).

I can place it in the working directory and use the code to get it when working in NetBeans.

DriverManager.getConnection("jdbc:ucanaccess://" + "res/mydatabase.accdb");

However, this does not work when I run the jar file (I am guessing this is because that folder location is no longer the working directory).

I have considered two approaches so far:

  • To place the database file in a package so when the jar file is generated, it is part of it. I have seen this work for image files but does not work for the database.

    DriverManager.getConnection("jdbc:ucanaccess://" + getClass.getResources("/res/mydatabase.accdb").getFile());
    

    This does not work. I also read here that you cannot open the database file directly from the copy imbedded in the runnable JAR file.

  • I have also seen examples where people have the absolute file path but I would rather not because I need to work on different computers. (Tell me if this is a better solution though)

My first question is: Where can I store the file so that it can be found when I run it from NetBeans and when I run it from the jar file?

My second question is: Is there a specific line of code that I must use to access it from that location?

Nyla of Arda
  • 106
  • 3
  • 9

1 Answers1

1

If you are looking for a "one true" literal path (relative or absolute) that will work not only for development but also for deployment to all possible target platforms, then you may be hoping for too much. You may need to settle for writing code that will derive (or prompt for) a path, perhaps based on the user.home System property.

Gord Thompson
  • 116,920
  • 32
  • 215
  • 418