在客户现场发现,oshi 在 window10 上运行的时候报错,报错内容如下
Caused by: java.lang.ExceptionInInitializerError
at oshi.driver.windows.perfmon.PhysicalDisk.queryDiskCounters(PhysicalDisk.java:80) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHWDiskStore.queryReadWriteStats(WindowsHWDiskStore.java:244) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHWDiskStore.getDisks(WindowsHWDiskStore.java:181) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer.getDiskStores(WindowsHardwareAbstractionLayer.java:76) ~[oshi-core-6.1.6.jar!/:6.1.6]
at com.drzk.utils.OSUtils.init(OSUtils.java:45) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.utils.OSUtils.<clinit>(OSUtils.java:21) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.utils.CompUtils.getPublicIp(CompUtils.java:53) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.initialize.BoxInitializingBean.afterPropertiesSet(BoxInitializingBean.java:60) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688) ~[spring-beans-4.3.25.RELEASE.jar!/:4.3.25.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626) ~[spring-beans-4.3.25.RELEASE.jar!/:4.3.25.RELEASE]
... 24 more
Caused by: java.lang.RuntimeException: Unexpected registry type 1, expected REG_DWORD
at com.sun.jna.platform.win32.Advapi32Util.registryGetIntValue(Advapi32Util.java:1149) ~[jna-platform-5.11.0.jar!/:5.11.0 (b0)]
at com.sun.jna.platform.win32.Advapi32Util.registryGetIntValue(Advapi32Util.java:1120) ~[jna-platform-5.11.0.jar!/:5.11.0 (b0)]
at com.sun.jna.platform.win32.Advapi32Util.registryGetIntValue(Advapi32Util.java:1095) ~[jna-platform-5.11.0.jar!/:5.11.0 (b0)]
at oshi.driver.windows.perfmon.PerfmonDisabled.isDisabled(PerfmonDisabled.java:63) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.driver.windows.perfmon.PerfmonDisabled.<clinit>(PerfmonDisabled.java:44) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.driver.windows.perfmon.PhysicalDisk.queryDiskCounters(PhysicalDisk.java:80) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHWDiskStore.queryReadWriteStats(WindowsHWDiskStore.java:244) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHWDiskStore.getDisks(WindowsHWDiskStore.java:181) ~[oshi-core-6.1.6.jar!/:6.1.6]
at oshi.hardware.platform.windows.WindowsHardwareAbstractionLayer.getDiskStores(WindowsHardwareAbstractionLayer.java:76) ~[oshi-core-6.1.6.jar!/:6.1.6]
at com.drzk.utils.OSUtils.init(OSUtils.java:45) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.utils.OSUtils.<clinit>(OSUtils.java:21) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.utils.CompUtils.getPublicIp(CompUtils.java:53) ~[box-utils-0.0.1-SNAPSHOT.jar!/:0.0.1-SNAPSHOT]
at com.drzk.initialize.BoxInitializingBean.afterPropertiesSet(BoxInitializingBean.java:60) ~[classes!/:0.0.1-SNAPSHOT]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1688) ~[spring-beans-4.3.25.RELEASE.jar!/:4.3.25.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1626) ~[spring-beans-4.3.25.RELEASE.jar!/:4.3.25.RELEASE]
... 24 more
在 GitHub issue找到解决方案
方案 1,修改注册表
将 计算机 \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\PerfOS\Performance
中Disable Performance Counters
的值类型修改为REG_DWORD
, 或者直接删除Disable Performance Counters
方案 2,改 oshi 的设置
在执行 oshi 方法之前,增加如下配置
GlobalConfig.set(GlobalConfig.OSHI_OS_WINDOWS_PERFOS_DIABLED, false);
GlobalConfig.set(GlobalConfig.OSHI_OS_WINDOWS_PERFPROC_DIABLED, false);
GlobalConfig.set(GlobalConfig.OSHI_OS_WINDOWS_PERFDISK_DIABLED, false);