当我使用OSHI API(https://github.com/oshi/oshi)时,我收到以下错误。
在我的Maven项目中,我有以下依赖项(我根据其他Stack Overflow帖子添加了SLF4J以修复错误,但没有起作用):
<!-- 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