如何使用Java编写系统偏好设置?我可以调用UAC吗? [英] How can I write System preferences with Java? Can I invoke UAC?

查看:144
本文介绍了如何使用Java编写系统偏好设置?我可以调用UAC吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 Preferences.systemRoot() ?

我尝试过:

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)

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

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.

该如何解决?我可以调用UAC并从Java中以管理员身份执行此操作吗?如果我在写入时以管理员身份登录,如果我以用户身份登录,是否可以使用Java应用程序读取值?

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?

推荐答案

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

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.

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

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.

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

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.

Sun Windows JDK附带了通用代码以写入注册表的任意部分(WindowsPreferences),但是它不是公开的. 本文描述了如何使用反射来访问此类.

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.

这篇关于如何使用Java编写系统偏好设置?我可以调用UAC吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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