1、先引入依赖包
<!-- 获取系统信息 -->
<dependency>
<groupId>com.github.oshi</groupId>
<artifactId>oshi-core</artifactId>
<version>3.9.1</version>
</dependency>
<dependency>
<groupId>net.java.dev.jna</groupId>
<artifactId>jna</artifactId>
<version>4.5.2</version>
</dependency>
问题:maven引用之后发现dependencies出现红色波浪线,删除再添加还是不能解决问题,几经周折,终于解决了问题。
解决方案如下:
注掉pom文件中的jar引用,保存,然后去掉注解,maven依赖上的红色波浪线即可消失。然后再把Work Offline勾选回去即可。
2、测试代码
解决问题之后,参考网上的代码如下
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import oshi.SystemInfo;
import oshi.hardware.*;
import oshi.hardware.CentralProcessor.TickType;
import oshi.software.os.*;
import oshi.software.os.OperatingSystem.ProcessSort;
import oshi.util.FormatUtil;
import oshi.util.Util;
import java.util.Arrays;
import java.util.List;
public class SystemInfoTest {
private static void printComputerSystem(final ComputerSystem computerSystem) {
System.out.println("manufacturer: " + computerSystem.getManufacturer());
System.out.println("model: " + computerSystem.getModel());
System.out.println("serialnumber: " + computerSystem.getSerialNumber());
final Firmware firmware = computerSystem.getFirmware();
System.out.println("firmware:");
System.out.println(" manufacturer: " + firmware.getManufacturer());
System.out.println(" name: " + firmware.getName());
System.out.println(" description: " + firmware.getDescription());
System.out.println(" version: " + firmware.getVersion());
// System.out.println(" release date: " + (firmware.getReleaseDate() == null ? "unknown"
// : firmware.getReleaseDate() == null ? "unknown" : FormatUtil.formatDate(firmware.getReleaseDate())));
final Baseboard baseboard = computerSystem.getBaseboard();
System.out.println("baseboard:");
System.out.println(" manufacturer: " + baseboard.getManufacturer());
System.out.println(" model: " + baseboard.getModel());
System.out.println(" version: " + baseboard.getVersion());
System.out.println(" serialnumber: " + baseboard.getSerialNumber());
}
private static void printProcessor(CentralProcessor processor) {
System.out.println(processor);
System.out.println(" " + processor.getPhysicalProcessorCount() + " physical CPU(s)");
System.out.println(" " + processor.getLogicalProcessorCount() + " logical CPU(s)");
System.out.println("Identifier: " + processor.getIdentifier());
System.out.println("ProcessorID: " + processor.getProcessorID());
}
private static void printMemory(GlobalMemory memory) {
System.out.println("以使用内存: " + FormatUtil.formatBytes(memory.getAvailable()) + "总共内存"
+ FormatUtil.formatBytes(memory.getTotal()));
System.out.println("Swap used: " + FormatUtil.formatBytes(memor