javaspringpom.xmloshi

NoClassDefFoundError for SystemInfo class in oshi-core


I have included oshi-core as a dependency in my project using Maven:

Dependency Structure

<dependencies>
    <dependency>
        <groupId>com.github.oshi</groupId>
        <artifactId>oshi-core</artifactId>
       <version>5.2.5</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>${project.groupId}</groupId>
        <artifactId>score-worker-monitor-api</artifactId>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-test</artifactId>
    </dependency>
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-all</artifactId>
    </dependency>
</dependencies>

However, I am seeing this error:

2020-10-08 11:15:49,688 [scoreWorkerScheduler-5] (TaskUtils.java:95) ERROR - Unexpected error occurred in scheduled task.
java.lang.NoClassDefFoundError: oshi/SystemInfo
    at io.cloudslang.worker.monitor.CpuPerProcess.measure(CpuPerProcess.java:34)
    at io.cloudslang.worker.monitor.PerfMetricCollectorImpl.collectMetric(PerfMetricCollectorImpl.java:53)
    at io.cloudslang.worker.monitor.service.WorkerMetricCollectorServiceImpl.collectPerfMetrics(WorkerMetricCollectorServiceImpl.java:42)
    at sun.reflect.GeneratedMethodAccessor37.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748) 

I don't understand the below line from OSHI-GitHub. I mean having the dependency inside oshi-core then why should I add again jna dependencies?

If you are using a parent (e.g. Spring Boot) that includes JNA as a dependency, override the jna.version property or equivalent

I am using a latest jna and jna-platform version inside oshi-core dependency.


Solution

  • The documentation on the OSHI website regarding a JNA NoClassDefFoundError is not relevant for this case, as it's OSHI itself that is not being found. The problem is in the package containing the first line of your stack trace:

    at io.cloudslang.worker.monitor.CpuPerProcess.measure(CpuPerProcess.java:34)
    

    It appears from this package name you're using CloudSlang score, however, the latest version on GitHub does not include the class CpuPerProcess. You may be working from a forked version of that repository.

    In that case, you need to ensure the oshi-core dependency is included in the pom.xml for your forked version of CloudSlang.

    While it's not particularly relevant to the error you're seeing, to answer your question about JNA version is that it JNA artifacts are included within OSHI as a transitive dependency. If you use the Spring starter parent (rather than just another dependency) then the Spring version of JNA takes precedence and you have to define this in your pom.xml to ensure Spring includes the correct version:

    <properties>
        <jna.version>5.6.0</jna.version>
    </properties>