windows 如何使用 Java 编写系统首选项?我可以调用 UAC 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2935933/
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
How can I write System preferences with Java? Can I invoke UAC?
提问by Jonas
How can I write system preferences with Java, using Preferences.systemRoot()
?
如何使用 Java 编写系统首选项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.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.
我想这样做,因为我想安装一个嵌入式JavaDB数据库,并让计算机上的多个用户与应用程序使用同一个数据库。
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?
如何解决这个问题?我可以调用 UAC 并以管理员身份从 Java 执行此操作吗?如果我在写入时以管理员身份登录,如果我以用户身份登录,我可以使用 Java 应用程序读取值吗?
回答by mdma
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.
您不能从 java 首选项写入任意注册表位置 - 所有首选项都存储在一个子项下Software\Javasoft\Prefs
。用户首选项映射到 HKEY_CURRENT_USER 配置单元,系统映射到HKEY_LOCAL_MACHINE
配置单元。
To write to the registry, you could use the windows "REG" command line tool. This pagedetails other ways of modifying the registry. including use of .reg
files.
要写入注册表,您可以使用 windows 的“REG”命令行工具。此页面详细介绍了修改注册表的其他方法。包括.reg
文件的使用。
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.
我有同样的需求 - 从 java 写入注册表 - 我通过编写一个小的 .NET 命令行实用程序来解决它。
The Sun Windows JDK does ship with generic code to write to arbitrary portions of the registry (WindowsPreferences), but it's not public. This articledescribes how to access this class using reflection.
Sun Windows JDK 确实附带通用代码来写入注册表的任意部分 (WindowsPreferences),但它不是公开的。本文介绍如何使用反射访问此类。
回答by Jared
回答by Jared
So I had this same issue, so I opened an issue with Oracle: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=7043176
所以我遇到了同样的问题,所以我用 Oracle 打开了一个问题:http: //bugs.sun.com/bugdatabase/view_bug.do?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
通过编写 AbstractPreferences 的自定义实现和相应的 PreferencesFactory,我能够自己解决这个问题。我所做的是在 Windows 上将系统首选项写入注册表中定义的应用程序数据目录: 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).
我曾经Runtime.getRuntime().exec("reg query \""+key+ "\" /v \""+value+"\"")
得到过(即使打开 UAC 也能工作)。
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.
在 Windows 7 上计算为“C:\ProgramData”,在 XP 上计算为“C:\Documents and Settings\All Users\Application Data”。我添加了一个名为“JavaPreferences”的子目录,并编写了一个使用属性文件作为后端的实现。
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.
附带说明一下,我在 Linux 上的系统首选项也有类似的问题,因为 JRE 的安装程序不是由 root 运行的,所以我无权访问“/etc/.java”。A 最终选择了另一个自定义目录并为其授予权限。