jRegistryKey 目前最新版本为:1.4.5
Java 使用 jRegistryKey 操作 Windows 注册表时抛出了以上问题,按照官方说的将 jRegistryKey.dll 文件放到 %SystemRoot%\system32 文件夹下面,导入相应 JAR 包即可,这种方式在 32 系统中可能运行通过(我在虚拟机XP系统中测试通过),但在 Windows 7(64 位)系统中却不行,查看一下Java 代码:
import ca.beq.util.win32.registry.RegistryKey;
import ca.beq.util.win32.registry.RegistryValue;
import ca.beq.util.win32.registry.RootKey;
public class RegEdit {
public static void main(String[] args) {
RegistryKey rk = new RegistryKey(RootKey.HKEY_LOCAL_MACHINE, "SOFTWARE\\Macromedia\\FlashPlayerPlugin");
String installPath = "";
if(rk.hasValue("UninstallerPath")) {
RegistryValue rv = rk.getValue("UninstallerPath");
installPath = rv.getData().toString();
}else{
System.err.println("查找的数值名称不存在!");
}
System.out.println(installPath);
}
}
导入的包路径为 ca.beq.util.win32 ,由此猜想 jRegistryKey 是不是只支持 32 系统而不支持 64 位系统?
Google 了一把有个帖子也是出现了这个问题,其中有人说:“第一次 new RegistryKey() 时,会通过 loadLibrary(...) 将dll文件装入内存,找不到时就会产生UnsatisfiedLinkError”,最后楼主自己解决了这个异常,但却没拿出来分享,想想也太不厚道!
但略有收获,毕竟上面这句话提醒了我,于是直接想通过 Java 在 new RegistryKey() 前先将dll文件加载到内存中:
System.loadLibrary("jRegistryKey.dll");
测试不通过,却报出以下异常:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no jRegistryKey.dll in java.library.path
最后通过以下语句输出 java.library.path ,获得的是环境变量中 Path 的值:
System.out.println(System.getProperty("java.library.path"));
D:\Program Files\Java\jdk1.6.0_30\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Program Files/MyEclipse/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin/client;D:/Program Files/MyEclipse/binary/com.sun.java.jdk.win32.x86_1.6.0.013/jre/bin;F:\ProgramFiles\Oracle\product\11.1.0\db_1\bin;C:\Program Files (x86)\AMD APP\bin\x86_64;。。。。。。
注意标红那句,恍然大悟后解决方法自然而来,将 jRegistryKey.dll 文件放到 jdk1.6.0_30\bin 目录下即可!
这样算不算是解决了兼容性的问题呢? O(∩_∩)O