Java - 设置首选项backingstore目录 [英] Java - Setting Preferences backingstore directory

查看:190
本文介绍了Java - 设置首选项backingstore目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在我的Java应用程序中创建持久存储,以便所有用户都可以访问它。所以我正在研究 java.util.prefs.Preferences 并使用 systemRoot()在Windows上正常工作,在Register注册数据。
但是我真的在Linux(Ubuntu)上面临一些问题。我想使用其他应用程序已经使用的目录:/ usr / share /。
所以,我试图在运行时将 systemRoot 默认目录重定向到/ usr / share。这是我的代码:

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.Preferences and 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 systemRoot default directory to /usr/share at runtime. Here's my code:

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

根据这个 site ,我必须创建 .systemPrefs 执行此命令行之前的文件夹及其隐含的 systemRoot()将使用它。

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

当我执行我的程序时,我得到以下警告:
java.util.prefs.FileSystemPreferences syncWorld
警告无法刷新系统首选项:java.util.prefs.BackingStoreException:/etc/.java /.systemPrefs/usr创建失败。

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.

所以我假设 System.setProperty 无法正常工作。有什么建议吗?
提前致谢!

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

推荐答案

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

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.

赔率是你的定义是在Linux系统中无效,因为它无法从/ etc启动。显然,Java代码中的某些内容违反了操作系统的规范,而不是您决定重新定义首选项根目录。

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

如果您希望任何用户阅读任何其他用户的偏好(说明听起来像您),那么您将需要一个以充分授权的用户(通常是root用户身份)运行的安装程序来制作 / etc 下的必需目录,并将其权限更改为全局可写。

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.

通常,/ 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.

这篇关于Java - 设置首选项backingstore目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆