我的程序使用java.util.Preferences
类(系统首选项,而不是用户)将加密的产品密钥数据保存到计算机上。问题是,在Windows和Linux上(尚未在OSX上进行测试,但可能相同),如果我不以sudo
或管理员权限运行程序,则每当尝试读取或保存数据时都会发出异常或警告。
显然,要求用户以管理员权限运行程序是不切实际的。最理想的情况是,我希望操作系统询问用户是否允许。
这很愚蠢,并且去掉了Preferences
的一半用途。如何解决这个问题?
这是我需要的简要概述:我需要我的程序请求操作系统允许保存系统设置。
以下是错误信息
当我尝试读取一个节点(因为该节点不存在)时,出现以下错误:
Mar 18, 2011 9:41:15 AM java.util.prefs.WindowsPreferences <init>
WARNING: Could not create windows registry node Software\JavaSoft\Prefs\myapp at root 0x80000002. Windows RegCreateKeyEx(...) returned error code 5.
Mar 18, 2011 9:41:15 AM java.util.prefs.WindowsPreferences WindowsRegOpenKey1
WARNING: Trying to recreate Windows registry node Software\JavaSoft\Prefs\myapp at root 0x80000002.
Mar 18, 2011 9:41:15 AM java.util.prefs.WindowsPreferences openKey
WARNING: Could not open windows registry node Software\JavaSoft\Prefs\myapp at root 0x80000002. Windows RegOpenKey(...) returned error code 2.
Mar 18, 2011 9:41:15 AM java.util.prefs.WindowsPreferences WindowsRegOpenKey1
WARNING: Trying to recreate Windows registry node Software\JavaSoft\Prefs\myapp\subpackage at root 0x80000002.
Mar 18, 2011 9:41:15 AM java.util.prefs.WindowsPreferences openKey
WARNING: Could not open windows registry node Software\JavaSoft\Prefs\myapp\subpackage at root 0x80000002. Windows RegOpenKey(...) returned error code 2.
当我尝试写入节点时,发生了以下情况:
Mar 18, 2011 9:43:11 AM java.util.prefs.WindowsPreferences WindowsRegOpenKey1
WARNING: Trying to recreate Windows registry node Software\JavaSoft\Prefs\myapp\subpackage at root 0x80000002.
Mar 18, 2011 9:43:11 AM java.util.prefs.WindowsPreferences openKey
WARNING: Could not open windows registry node Software\JavaSoft\Prefs\myapp\subpackage at root 0x80000002. Windows RegOpenKey(...) returned error code 2.
Preferences
类中的方法只有在存在安全管理器时才会抛出SecurityException
异常。那么在您的代码中会抛出什么类型的异常呢? - Matt Ball