Java preferences api

Has anyone used the Java Preferences API with snaps? I’m hoping there is an environment variable that can be set to point the preferences storage inside SNAP_USER_DATA. If I use the preferences API as is, I get permissions errors:

Apr 29, 2019 12:13:10 PM java.util.prefs.FileSystemPreferences$6 run
WARNING: Prefs file removed in background /home/jsseidel/.java/.userPrefs/_!':!bw"t!#4!cw"w!'`!bg"j!'`!cg"z!'`!a@"k!'`!b!!u!%o![w"h!'w!}@"4!'k!}@"u/prefs.xml
Exception in thread "Sync Timer Thread" java.lang.SecurityException: Could not lock User prefs. Lock file access denied.

I think this happens because snaps cannot access dot files in the home directory.

Any ideas?

Answering my own question. You can set the values for java.utils.prefs.userRoot and java.utils.prefs.systemRoot like in this example:

java -Djava.util.prefs.userRoot="$SNAP_USER_DATA" -jar some.jar

In your java run wrapper. This allows the normal Java Preferences API to work with snaps.

2 Likes