2

I am using system prefrences for testing my code i observed some peculiar things on java 1.5

private void loadEmptyPreferences() throws IOException,
            InvalidPreferencesFormatException, BackingStoreException {
        ClassLoader contextClassLoader = Thread.currentThread()
                .getContextClassLoader();
        InputStream stream = contextClassLoader
                .getResourceAsStream("example.xml");
        if (stream == null) {
            fail("Could not load preferences file");
        }
        Preferences pref = Preferences.systemRoot().node("test");
        pref.removeNode();
        Preferences.importPreferences(stream);
    }

I get

java.lang.SecurityException: Could not lock System prefs.Lock file access denied. at java.util.prefs.FileSystemPreferences.checkLockFile0ErrorCode(FileSystemPreferences.java:919) at java.util.prefs.FileSystemPreferences.lockFile(FileSystemPreferences.java:908) at java.util.prefs.FileSystemPreferences.removeNode(FileSystemPreferences.java:656)

The reason is that the preferences-systems tries to write a lock file at a location where "normal" users have no write permissions on Linux (/opt/j2se/linux/ix86/j2se_1.5.0_22/jre/.systemPrefs/.system.lock)

How to solve this?Should we use properties?

constantlearner
  • 5,157
  • 7
  • 42
  • 64
  • possible duplicate of [Java - Setting Preferences backingstore directory](http://stackoverflow.com/questions/15004954/java-setting-preferences-backingstore-directory) – joran Feb 24 '14 at 22:05

2 Answers2

2

Preferences.systemRoot() returns the system-wide (i.e. root only) preferences. They can't be changed by non-privileged users.

If you need to change settings for normal users, try using userRoot() instead.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • I have the same problem on an Ubuntu virtual box, without using Preferences.systemRoot() in my application. This seems to come from JVM API instead. – LD. Apr 01 '13 at 16:33
1

This is a really pesky issue Java running on *nix based servers.

I was able to solve it by using the following vm args:

-Djava.util.prefs.userRoot=/opt/apache-tomcat-7.0.50/uprefs -Djava.util.prefs.systemRoot=/opt/apache-tomcat-7.0.50/sprefs

One important note though on the systemRoot path is to create a sub-folder within it named .systemPrefs or it will not work.

Also, don't forget to chown -R these directories to the user running the java application (in my case it was tomcat).

okrunner
  • 3,083
  • 29
  • 22