oshi-core 中 SystemInfo class 的 NoClassDefFoundError

NoClassDefFoundError for SystemInfo class in oshi-core

我使用 Maven 将 oshi-core 作为依赖包含在我的项目中:

依赖结构

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

但是,我看到了这个错误:

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.access1(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) 

我不明白 OSHI-GitHub 的下面一行。我的意思是在 oshi-core 中有依赖项,那我为什么要再次添加 jna 依赖项?

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

我在 oshi-core 依赖项中使用最新的 jna 和 jna-platform 版本。

OSHI 网站上关于 JNA NoClassDefFoundError 的文档与本案例无关,因为未找到 OSHI 本身。问题出在包含堆栈跟踪第一行的包中:

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

从这个包名称看来您正在使用 CloudSlang score,但是 GitHub 上的最新版本不包括 class CpuPerProcess。您可能正在使用该存储库的分叉版本。

在这种情况下,您需要确保 oshi-core 依赖项包含在您的 CloudSlang 分叉版本的 pom.xml 中。

虽然它与您看到的错误并不特别相关,但要回答您关于 JNA 版本的问题是 JNA 工件作为传递依赖项包含在 OSHI 中。如果您使用 Spring 起始父级(而不仅仅是另一个依赖项),那么 JNA 的 Spring 版本优先并且您必须在 pom.xml 中定义它以确保 Spring包括正确的版本:

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