javajnanosuchmethoderroroshi

Java OSHI (Operating System and Hardware Information) library NoSuchMethodError


I receive the following error when using the OSHI API (https://github.com/oshi/oshi).

I have the following dependencies in my Maven project (I added SLF4J to fix the error based on other Stack Overflow posts, but didn't help):

<!-- 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>

This is how I use 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)) );
        }
    }
}

This is the error code I receive:

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

Solution

  • OSHI 6.2.2 requires JNA (Java Native Access) 5.12.1, but it looks like you have an older version of JNA on the classpath. The Memory.close() method was introduced in JNA 5.12.0.

    Check with mvn dependency:tree which dependencies are pulled in for net.java.dev.jna:jna and net.java.dev.jna:jna-platform, and either add an explicit dependency in your own POM (in dependencyManagement), or exclude it from the library that pulls in an older version, so the version defined by OSHI is pulled in.