英文:
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)
...
我不理解来自OSHIGitHub的下面一行。我的意思是,既然在oshi-core中已经有了依赖关系,为什么我还需要再次添加jna的依赖项?
如果您使用的是已包含JNA依赖项的父项(例如Spring Boot),请覆盖jna.version属性或等效属性
我在oshi-core的依赖项中使用了最新的jna和jna-platform版本。
英文:
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.
答案1
得分: 1
在 OSHI 网站上关于 JNA 的 NoClassDefFoundError
的文档与此情况无关,因为无法找到的是 OSHI 本身。问题出现在包含您堆栈跟踪第一行的包中:
at io.cloudslang.worker.monitor.CpuPerProcess.measure(CpuPerProcess.java:34)
从这个包名看,您正在使用 CloudSlang score,然而,GitHub 上的最新版本不包括 CpuPerProcess
类。您可能正在使用该存储库的派生版本。
在这种情况下,您需要确保在您派生的 CloudSlang 版本的 pom.xml
中包含 oshi-core
依赖。
尽管与您所看到的错误不是特别相关,但是要回答您关于 JNA 版本的问题,JNA 构件作为 OSHI 的传递性依赖项包含在内。如果您使用 Spring Starter Parent(而不仅仅是另一个依赖项),那么 Spring 版本的 JNA 会优先,并且您必须在您的 pom.xml
中定义如下内容,以确保 Spring 包含正确的版本:
<properties>
<jna.version>5.6.0</jna.version>
</properties>
英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论