方法1:通过com.registry》JRegister
兼容的JDK版本:jregistry-1.8.1测试过可以直接jdk1.8,其他官方文档不详。
所需jar包:jregistry-1.8.1.jar在源码中配置好, reg_x64.dll或者reg.dll需要拷贝到jdk的bin目录中(要保持jdk与dll的版本与操作系统一致)。
下载地址:https://pan.baidu.com/s/1kViGmRd
代码示例:
import com.registry.RegistryKey;
import com.registry.RegistryValue;
public class TestRegister
{
public static void main(String[] args)
{
//"HKEY_LOCAL_MACHINE"
RegistryKey LOCALMACHINE = RegistryKey.getRootKeyForIndex(RegistryKey.HKEY_LOCAL_MACHINE_INDEX);
//"HKEY_LOCAL_MACHINE\SOFTWARE\\MySQL AB\MySQL Workbench 6.3 CE\"
RegistryKey orclKey = new RegistryKey(LOCALMACHINE, "\\SOFTWARE\\MySQL AB\\MySQL Workbench 6.3 CE\\");
String version= null;
System.out.println("orclKey.hasValues=" + orclKey.hasValues());
if(orclKey.hasValues())
{
RegistryValue portNumber = orclKey.getValue("Version");
System.out.println(portNumber.toString()); // Name: PortNumber Type: REG_DWORD Value: 3389
String value = portNumber.toString();
port = value.substring(value.lastIndexOf(": ")+1, value.length()).trim();
System.out.println("version=" + version);
}
}
}
输出结果:
orclKey.hasValues=true
Name: Version Type: REG_SZ Value: 6.3.7
version=6.3.7
方法2:通过com.ice.jni.registry》Register
兼容的JDK版本:JDK1.2, and JDK1.3, JDK1.4, as well as JDK1.1.8
所需jar包: Registry.jar需要配置到源码中,相应的dll文件同样放到jdk的bin‘’文件夹中(要保持jdk与dll的版本与操作系统一致)。
下载地址: http://www.trustice.com/java/jnireg/index.shtml
参考博客: http://blog.chinaunix.net/uid-21227800-id-65826.html
代码示例:
import com.ice.jni.registry.*;
public void test()
{
RegistryKey registryKey = null;
try
{
//将ICE_JNIRegistry.dll拷贝到java.library.path其中任意一个目录下即可
System.out.println("" + System.getProperties().get("java.library.path"));
System.loadLibrary("ICE_JNIRegistry");
registryKey = Registry.openSubkey(Registry.HKEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\services\\postgresql-9.4",
RegistryKey.ACCESS_READ);
System.out.println(registryKey.getStringValue("DisplayName"));
} catch (NoSuchKeyException e)
{
logger.error(e.getMessage());
e.printStackTrace();
}
catch (RegistryException e)
{
logger.error(e.getMessage());
e.printStackTrace();
} finally
{
if (registryKey != null)
{
try
{
registryKey.closeKey();
} catch (RegistryException ex)
{
logger.error(ex.getMessage());
ex.printStackTrace();
}
}
}
}
输出结果:
D:\Java32\jdk1.7.0_67\jre\bin;C:\Windows\Sun\Java\bin;D:\Java32\jdk1.7.0_67\bin;.
postgresql-9.4 - PostgreSQL Server 9.4