Java - 设置首选项 backingstore 目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15004954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 18:10:23  来源:igfitidea点击:

Java - Setting Preferences backingstore directory

javalinuxjvmpreferences

提问by Leonardo

I need to create a persistent storage in my Java app so all users can access it. So I was studying about java.util.prefs.Preferencesand using systemRoot()works fine to me on Windows, saving data at Register. But I'm really facing some problems on Linux (Ubuntu). I want to use a directory that other of our apps already uses: /usr/share/. So, I'm trying to redirect systemRootdefault directory to /usr/share at runtime. Here's my code:

我需要在我的 Java 应用程序中创建一个持久存储,以便所有用户都可以访问它。所以我在 Windows 上研究java.util.prefs.Preferences和使用systemRoot()对我来说很好用,在 Register 上保存数据。但是我在 Linux (Ubuntu) 上确实遇到了一些问题。我想使用我们的其他应用程序已经使用的目录:/usr/share/。所以,我试图systemRoot在运行时将默认目录重定向到 /usr/share。这是我的代码:

System.setProperty("java -Djava.util.prefs.systemRoot", "/usr/share/myfolder");
Preferences pref = Preferences.systemRoot().node("/usr/share/myfolder");

According to this site, I have to create .systemPrefsfolder before execute this command line and its implicit that systemRoot()will use it.

根据此站点,我必须.systemPrefs在执行此命令行之前创建文件夹及其隐式systemRoot()将使用它。

When I execute my program, I get the following WARNING: java.util.prefs.FileSystemPreferences syncWorld WARNING Couldn't flush system prefs: java.util.prefs.BackingStoreException: /etc/.java/.systemPrefs/usr create failed.

当我执行我的程序时,我收到以下警告: java.util.prefs.FileSystemPreferences syncWorld WARNING Couldn't flush system prefs: java.util.prefs.BackingStoreException: /etc/.java/.systemPrefs/usr create failed.

So I'm assuming that System.setPropertyisn't working. Any suggestion? Thanks in advance!

所以我假设这System.setProperty不起作用。有什么建议吗?提前致谢!

回答by StevieP

Bimalesh suggested that instead of

Bimalesh 建议,而不是

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

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

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

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

但是您要设置的属性的名称是java.util.prefs.systemRoot,而不是-Djava.util.prefs.systemRoot,所以您应该这样做

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

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

如果这不起作用,请尝试将“-D”开关添加到启动程序的命令行。那是 java -D...应该去的地方。该命令将以

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

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

回答by acohen

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

这是在基于 *nix 的服务器上运行的 Java 非常讨厌的问题。

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

我能够通过使用以下 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 systemRootpath is to create a sub-folder within it named .systemPrefsor it will not work.

systemRoot路径上的一个重要注意事项是在其中创建一个名为的子文件夹,.systemPrefs否则它将无法工作。

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

另外,不要忘记将chown -R这些目录提供给运行 java 应用程序的用户(在我的情况下是 tomcat)。

回答by Edwin Buck

In a Linux system, the Systemroot 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.

在 Linux 系统中,系统根首选项节点将位于/etc. 这是由于历史原因,并且是由 Linux 标准库规范的标准。任何非系统首选项都可以放在其他位置,但是将系统首选项放在其他位置是违反操作系统设计的。

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.

很可能您的定义在 Linux 系统中无效,因为它无法从 /etc 启动。显然,Java 代码中的某些内容遵循操作系统的规范,而不是您决定重新设置首选项根的基础。

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 /etcand change it's permissions to be world writeable.

如果您希望任何用户阅读任何其他用户的首选项(描述听起来像您可能会这样做),那么您将需要一个安装程序以足够授权的用户(通常是 root)身份运行,以创建所需的目录/etc并将其权限更改为世界可写。

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.

通常 /etc 下的文件是不可写入的,因为用户可能会更改其他用户的设置,并且被视为对用户预期环境的一种安全破坏。例如,一个粗心的员工(或一个心怀不满的员工)可能会一次性抹掉所有其他用户的偏好。