views:

91

answers:

1

Once I deploy my application with JPA the user chooses to install it somewhere. Then however the property set as:

<property name="javax.persistence.jdbc.url" value="jdbc:derby:db;create=true"/>

gets interpreted into the following exception: couldn't create database in \db. Throughout development it used to be the relative path to the project folder, and not the root as it's now. What should I do to make the path remain relative to the folder in which the application is installed? Or at the very worse, the userdir.

A: 

You should write the install location somewhere and set the derby.system.home system property to this location before creating the connection. Quoting the Using Java DB in Desktop Applications article:

Connecting to the Java DB Database

...

All connection URLs have the following form:

jdbc:derby:<dbName>[propertyList]

The dbName portion of the URL identifies a specific database. A database can be in one of many locations: in the current working directory, on the classpath, in a JAR file, in a specific Java DB database home directory, or in an absolute location on your file system. The easiest way to manage your database location in an embedded environment is to set the derby.system.home system property. This property tells Java DB the default home location of all databases. By setting this property, the Address Book demo ensures that Java DB always finds the correct application database. The application database is named DefaultAddressBook, and it will exist within the directory indicated by the derby.system.home property. The connection URL for this database would look like this:

jdbc:derby:DefaultAddressBook

...

To connect to the DefaultAddressBook database, the demo must first set the derby.system.home system property. The demo uses the .addressbook subdirectory of the user's home directory. Use the System class to find out the user's home directory. Then use the class again to set the derby.system.home property:

private void setDBSystemDir() {
    // Decide on the db system directory: <userhome>/.addressbook/
    String userHomeDir = System.getProperty("user.home", ".");
    String systemDir = userHomeDir + "/.addressbook";

    // Set the db system directory.
    System.setProperty("derby.system.home", systemDir);
}
Pascal Thivent
I cannot know the installation directory (it's handled by a 3rd party installer). The wierd thing is that the current directory, i.e. new File(".") is root!
simpatico
@simpatico: Then maybe use the user's home. PS: A good installer should store the installation path somewhere (for uninstall).
Pascal Thivent
Even that is taken as /!I think this is a problem by itself. Why doesn't user.dir reflect the actual working directory?
simpatico
@simpatico - *"Why doesn't user.dir reflect the actual working directory? "* Perhaps it does reflect th actual working directory! It depends (in part) on the nature of the application, and the way that it is launched. For instance, a wrapper might change directory to "/" before launching the application.
Stephen C