3

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
Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
Thend
  • 95
  • 7
  • 1
    The NoSuchMethodError has nothing to do with SLF4J. It means that you likely have the wrong (too old) version of JNA (Java Native Access) on your classpath. The lines prefixed with `SLF4J` are just *warnings* that you don't have a proper logging library configured for SLF4J. – Mark Rotteveel Aug 10 '22 at 07:58
  • I changed JNA version, however still something wrong. Please check my UPDATE. Thx! – Thend Aug 10 '22 at 08:26
  • That indicates the problem is solved. If you're referring to those SLF4J lines, as I said, the loglines prefixed with SLF4J are caused by the fact you don't have a proper logging library configured for SLF4J (you only have slf4j-simple on test scope, so there is no logging library for main scope) – Mark Rotteveel Aug 10 '22 at 08:37
  • In any case, that is a different problem, and I will rollback your edit, as you shouldn't move the goalposts of the question. The problem about the SLF4J lines is addressed by multiple questions on Stack Overflow, like [this one](https://stackoverflow.com/questions/7421612/slf4j-failed-to-load-class-org-slf4j-impl-staticloggerbinder) – Mark Rotteveel Aug 10 '22 at 08:37
  • Does this answer your question? [NoSuchMethodError in jna-platform](https://stackoverflow.com/questions/55982008/nosuchmethoderror-in-jna-platform) – Daniel Widdis Aug 11 '22 at 19:00

1 Answers1

4

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.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197