Java - Setting Preferences backingstore directory

感情迁移 提交于 2019-12-01 09:24:01

In a Linux system, the System root preference node will be under /etc. This is due to history, and is a standard that is regulated by the Linux Standard Base. Any non-system preferences can go in other locations, but it is a violation of the design of the operating system to have system preference go elsewhere.

Odds are your define is ineffective in a Linux system because it fails to start at /etc. Apparently something in the Java code defers to the specification of the operating system over your decision to re-base the preference root.

Typically such files are protected against modification by not being world (or even most user) writeable. This means that for users to have access to Preferences, they should go under

 Preferences.userRoot()

Which will place them in hidden directories just off their home directory (where they will have modification privileges).

If your want any user to read any other user's preferences (the description sounds like you might) then you will need to have an installer that runs as a sufficiently authorized user (typically root) to make the required directory under /etc and change it's permissions to be world writeable.

Typically files under /etc are not world writable as users changing other's user's setting is then possible, and considered a type of security breach of the user's expected environment. For example, a careless employee (or a disgruntled one) could wipe out all other user's preferences in one stroke.

StevieP

Bimalesh suggested that instead of

System.setProperty("java -Djava.util.prefs.systemRoot", "/usr/share/myfolder"), that you say

System.setProperty("-Djava.util.prefs.systemRoot", "/usr/share/myfolder").

But the name of the property that you are trying to set is java.util.prefs.systemRoot, and not -Djava.util.prefs.systemRoot, so you should do

System.setProperty("java.util.prefs.systemRoot", "/usr/share/myfolder");

If that doesn't work, try adding the "-D" switch to the command line that starts your program. That is where java -D... should go. The command would start with

java -Djava.util.prefs.systemRoot=/usr/share/myfolder

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).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!