使用OSHI API时收到以下错误(https://github.com/oshi/oshi)。
在我的Maven项目中,我有以下依赖项(我添加了SLF4J来修复基于其他Stack Overflow帖子的错误,但没有帮助(:
<!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>6.2.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
<!-- OSHI requires it!-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.36</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-simple -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.36</version>
<scope>test</scope>
</dependency>
这就是我使用OSHI:的方式
package controller;
import oshi.SystemInfo;
import oshi.hardware.CentralProcessor;
import oshi.hardware.HardwareAbstractionLayer;
public class SystemLoad extends Thread {
public SystemLoad() {
}
@Override
public void run(){
while(true) {
SystemInfo si = new SystemInfo();
HardwareAbstractionLayer hal = si.getHardware();
CentralProcessor cpu = hal.getProcessor();
System.out.println( String.valueOf(cpu.getSystemCpuLoad(100)) );
}
}
}
这是我收到的错误代码:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Exception in thread "Thread-0" java.lang.NoSuchMethodError: 'void com.sun.jna.Memory.close()'
at com.github.oshi@6.2.2/oshi.util.Util.freeMemory(Util.java:112)
at com.github.oshi@6.2.2/oshi.jna.ByRef$CloseableLONGLONGByReference.close(ByRef.java:101)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfDataUtil.updateQueryTimestamp(PerfDataUtil.java:174)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterQueryHandler.updateQuery(PerfCounterQueryHandler.java:134)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(PerfCounterWildcardQuery.java:164)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterWildcardQuery.queryInstancesAndValues(PerfCounterWildcardQuery.java:87)
at com.github.oshi@6.2.2/oshi.driver.windows.perfmon.ProcessorInformation.queryProcessorCounters(ProcessorInformation.java:167)
at com.github.oshi@6.2.2/oshi.hardware.platform.windows.WindowsCentralProcessor.queryProcessorCpuLoadTicks(WindowsCentralProcessor.java:349)
at com.github.oshi@6.2.2/oshi.util.Memoizer$1.get(Memoizer.java:87)
at com.github.oshi@6.2.2/oshi.hardware.common.AbstractCentralProcessor.getProcessorCpuLoadTicks(AbstractCentralProcessor.java:204)
at com.github.oshi@6.2.2/oshi.hardware.platform.windows.WindowsCentralProcessor.querySystemCpuLoadTicks(WindowsCentralProcessor.java:221)
at com.github.oshi@6.2.2/oshi.util.Memoizer$1.get(Memoizer.java:87)
at com.github.oshi@6.2.2/oshi.hardware.common.AbstractCentralProcessor.getSystemCpuLoadTicks(AbstractCentralProcessor.java:192)
at com.github.oshi@6.2.2/oshi.hardware.CentralProcessor.getSystemCpuLoad(CentralProcessor.java:189)
at hu.jhasher/controller.SystemLoad.run(SystemLoad.java:24)
Suppressed: java.lang.NoSuchMethodError: 'void com.sun.jna.Memory.close()'
at com.github.oshi@6.2.2/oshi.util.Util.freeMemory(Util.java:112)
at com.github.oshi@6.2.2/oshi.jna.ByRef$CloseableHANDLEByReference.close(ByRef.java:115)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterQueryHandler.removeAllCounters(PerfCounterQueryHandler.java:112)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterQueryHandler.close(PerfCounterQueryHandler.java:166)
at com.github.oshi@6.2.2/oshi.util.platform.windows.PerfCounterWildcardQuery.queryInstancesAndValuesFromPDH(PerfCounterWildcardQuery.java:146)
... 10 more
OSHI 6.2.2需要JNA(Java Native Access(5.12.1,但类路径上似乎有一个旧版本的JNA。在JNA 5.12.0中介绍了CCD_ 1方法。
与mvn dependency:tree
一起检查net.java.dev.jna:jna
和net.java.dev.jna:jna-platform
的依赖项,然后在您自己的POM中(在dependencyManagement
中(添加一个显式依赖项,或者将其从拉入旧版本的库中排除,以便拉入OSHI定义的版本。