Search code examples
javafx-2preferencesself-contained

JavaFx 2 - Self Contained Applications and their preferences, database, etc


Let say i have a cross-platform runnable application

This application create then read/write some data and preference in external files

Bundle hierarchy is as follow:

ApplicationFolder/application.jar
ApplicationFolder/database.odb
ApplicationFolder/config.xml

Whether it's on a Mac, Windows or Linux, the application knows that everything is next to her (ie: /database.odb or /config.xml)


Now comes the Self Contained Application feature provided by JavaFx 2

The application is embedded in .exe on Windows, .app on Mac and don't know yet about Linux...

As a Mac user i've tested it on Mac and saw that database.odb and config.xml are now created at the user root path

I thus agree that i should think of a cross-platform mechanism to save/read my application preferences regarding the operating system

But i'm not quite sure of what to do and how to do it (can't find any googling help either..)


On windows, the .exe is installed in a folder, so i guess i can keep the same behavior

On Mac, the .app is a folder and i should keep everything inside (how to get the .app path ?!)

Isn't there a built-in mechanism in Java/JavaFx ?


Thanks a lot for any comment, advice, documentation or else that you could give me

Badisi


Solution

  • There are many ways to do this. I have listed some of them here in no particular order. The recommended approach depends on the type of data being stored.

    Java provides a couple of mechanisms (e.g. the properties API and the preferences API) for maintaining application preferences.

    If your application is sophisticated enough to benefit from an database, then you might want to use Java EE or Spring, both of which have their own configuration mechanisms.

    For read-only configuration, you can bundle the relevant files inside your application jar.

    To store customized application configuration files or client application wide databases in relative to the application jar, write the required files at runtime. See How do I get the directory that the currently executing jar file is in?.

    For user specific configuration, use System.getProperty("user.home") to retrieve the user's home directory, then create a subdirectory for your preference storage (for example "{$user.dir}/.myapp") with hidden file attributes so that it doesn't show up on a standard file directory list.

    If your app relies on internet connectivity, then you can store some of this information server side rather than the client and make use of it from the client using internet protocols. An advantage of this approach is that user configuration and data is automatically ported across client machines.