3

While creating a Java solution that shall be deployed at least on Windows, Linux, Mac OS and Android we are looking for a way to keep system wide configuration data.

in What is the proper way to store app's conf data in Java? the solutions are discussed around a specific user's home directory. On one of Oracle's documentation pages:

http://docs.oracle.com/javase/7/docs/technotes/guides/deployment/deployment-guide/properties.html

a similiar approach ist taken.

We'd like to go a more system wide route. E.g. on a linux system a subdirectory of /etc or /var/lib might be a suitable location. On Windows /$WIN/System32 might be a start, on Mac OS /System/Library might be the location.

JAVA_HOME - might be an option but has it's own issues: Windows ignores JAVA_HOME: how to set JDK as default?

The java.util.Preference class has been around a while but id does not supply a proper choice of BackingStore locations: http://www.allaboutbalance.com/articles/disableprefs/

What would be a library that allows to

  • find out the proper system wide directory
  • add an application specific subfolder
  • and may be handle the properties (this could be done with regular properties files or java.util.Preferences)

The first part of the question is about the proper location in a platform independent approach. What directories should be used e.g. on Android? And what followup problems have to be handled (e.g. access rights)? The assumption would be that if a library exists the first part of the question would be answered by the way the library deals with this part of the question. So if you think there is no such library you are invited to give your answer to this part of the question.

Community
  • 1
  • 1
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
  • 1
    There is no such platform independent concept. Does the configuration really have to be system wide? Strikes me that unless you are concerned about multi-user systems, that Java Preferences would be as about a platform independent library that you could use. – Perception Apr 20 '13 at 13:35
  • Have you considered `System.getProperty("java.home")`? – OldCurmudgeon Apr 20 '13 at 14:50

1 Answers1

0

The approach at: http://www.java-tutorial.ch/core-java-tutorial/java-osname-system-property is the closest I could find so far:

String osName = System.getProperty("osName", null);
OperatingSystem  operatingSystem = OperatingSystem.resolve(osName);
OperatingSystemFamily family = operatingSystem.getFamily();
switch (family) {
  case UNIX:
    setBinFolder(DEFAULT_UNIX_BIN);
    setConfFolder(DEFAULT_UNIX_CONF);
    setDataFolder(DEFAULT_UNIX_DATA);
  break;
  case WINDOWS:
    setBinFolder(DEFAULT_WINDOWS_BIN);
    setConfFolder(DEFAULT_WINDOWS_CONF);
    setDataFolder(DEFAULT_WINDOWS_DATA);
  break;
  case MACOS:
    setBinFolder(DEFAULT_MACOS_BIN);
    setConfFolder(DEFAULT_MACOS_CONF);
    setDataFolder(DEFAULT_MACOS_DATA);
   break;
  default:
    setBinFolder(USER_HOME+"/bin");
    setConfFolder(USER_HOME+"/conf");
    setDataFolder(USER_HOME+"/data");                   
  break;
}

but it only solves the part of the problem to identify the OS and react accordingly. Feel free to build on this to answer the question.

Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186