如何使用 Java 编写系统偏好设置?我可以调用UAC吗?

发布于 2024-09-03 07:05:59 字数 1310 浏览 6 评论 0原文

如何使用 Java 编写系统首选项,使用 Preferences.systemRoot()

我尝试过:

Preferences preferences = Preferences.systemRoot();
preferences.put("/myapplication/databasepath", pathToDatabase);

但我收到此错误消息:

2010-maj-29 19:02:50 java.util.prefs.WindowsPreferences openKey
VARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002. Windows RegOpenKey(...) returned error code 5.
Exception in thread "AWT-EventQueue-0" java.lang.SecurityException: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002: Access denied
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.putSpi(Unknown Source)
    at java.util.prefs.AbstractPreferences.put(Unknown Source)
    at org.example.install.Setup$2.actionPerformed(Setup.java:43)

我想这样做,因为我想安装嵌入式 JavaDB 数据库,并让计算机上的多个用户与应用程序使用同一个数据库。

怎么解决这个问题呢?我可以调用 UAC 并以管理员身份从 Java 执行此操作吗?如果我在写入时以管理员身份登录,那么如果我以用户身份登录,我可以使用 Java 应用程序读取这些值吗?

How can I write system preferences with Java, using Preferences.systemRoot()?

I tried with:

Preferences preferences = Preferences.systemRoot();
preferences.put("/myapplication/databasepath", pathToDatabase);

But I got this error message:

2010-maj-29 19:02:50 java.util.prefs.WindowsPreferences openKey
VARNING: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002. Windows RegOpenKey(...) returned error code 5.
Exception in thread "AWT-EventQueue-0" java.lang.SecurityException: Could not open windows registry node Software\JavaSoft\Prefs at root 0x80000002: Access denied
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.openKey(Unknown Source)
    at java.util.prefs.WindowsPreferences.putSpi(Unknown Source)
    at java.util.prefs.AbstractPreferences.put(Unknown Source)
    at org.example.install.Setup$2.actionPerformed(Setup.java:43)

I would like to do this, because I want to install an embedded JavaDB database, and let multiple users on the computer to use the same database with the application.

How to solve this? Can I invoke UAC and do this as Administrator from Java? And if I log in as Administrator when writing, can I read the values with my Java application if I'm logged in as a User?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

毁梦 2024-09-10 07:06:00

所以我也遇到了同样的问题,所以我向 Oracle 提出了一个问题:
https://bugs.java.com/bugdatabase/view_bug?bug_id=7043176

我自己可以通过编写 AbstractPreferences 的自定义实现和相应的 PreferencesFactory 来解决这个问题。我在 Windows 上所做的是通过以下方式将系统首选项写入注册表中定义的应用程序数据目录:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common AppData

我使用 Runtime.getRuntime().exec("reg query \""+key+ "\" /v \""+value+"\ "") 来获取它(即使 UAC 打开也可以工作)。

在 Windows 7 上,其计算结果为“C:\ProgramData”;在 XP 上,计算结果为“C:\Documents and Settings\All Users\Application Data”。我添加了一个名为“JavaPreferences”的子目录,并编写了一个使用属性文件作为后端的实现。

附带说明一下,我在 Linux 上的系统首选项上遇到了类似的问题,因为 JRE 的安装程序不是由 root 运行的,所以我无法访问“/etc/.java”。 A 最终选择了另一个自定义目录并授予了该目录的权限。

So I had this same issue, so I opened an issue with Oracle:
https://bugs.java.com/bugdatabase/view_bug?bug_id=7043176

I was able to work around it myself, by writing a custom implementation of AbstractPreferences and a corresponding PreferencesFactory. What I did was on Windows have the system preferences write to the application data directory defined in the registry by:
HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Common AppData

I used Runtime.getRuntime().exec("reg query \""+key+ "\" /v \""+value+"\"") to get that (works even with UAC turned on).

That evaluates to "C:\ProgramData" on Windows 7 and "C:\Documents and Settings\All Users\Application Data" on XP. I added a subdirectory called "JavaPreferences" and wrote an implementation that uses a properties file as the backend.

As a side note, I had a similar issue with system preferences on Linux because the installer for the JRE was not run by root so I didn't have access to "/etc/.java". A ended up picking another custom directory and granting permissions for that.

潇烟暮雨 2024-09-10 07:05:59

您无法从 java 首选项写入任何任意注册表位置 - 所有首选项都存储在子项 Software\Javasoft\Prefs 下。用户首选项映射到 HKEY_CURRENT_USER 配置单元,系统映射到 HKEY_LOCAL_MACHINE 配置单元。

要写入注册表,您可以使用 Windows“REG”命令行工具。 本页详细介绍了修改注册表的其他方法。包括使用 .reg 文件。

我有同样的需求 - 从 java 写入注册表 - 我通过编写一个小的 .NET 命令行实用程序来解决它。

Sun Windows JDK 确实附带了用于写入注册表任意部分(WindowsPreferences)的通用代码,但它不是公开的。 本文介绍如何使用反射访问此类。

You cannot write to any arbitrary registry location from java preferences - all preferences are stored under a subkey Software\Javasoft\Prefs. With user preferences mapping to the HKEY_CURRENT_USER hive, and system mapping to the HKEY_LOCAL_MACHINE hive.

To write to the registry, you could use the windows "REG" command line tool. This page details other ways of modifying the registry. including use of .reg files.

I had the same need - to write to the registry from java - I solved it by writing a small .NET command line utility to do it.

The Sun Windows JDK does ship with generic code to write to arbitrary portions of the registry (WindowsPreferences), but it's not public. This article describes how to access this class using reflection.

禾厶谷欠 2024-09-10 07:05:59

如果用户帐户控制已打开,则无法编辑 Preferences.systemRoot()。看来微软已经打破了它。有一个解决方法 这里,但这并不简单。

You can't edit Preferences.systemRoot() if User Account Control is turned on. Seems like Microsoft went and broke it. There is a workaround here, but it's not straightforward.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文