一般的都会考虑到sigar,但是sigar需要 dll 文件和 so文件等动态链接库,比较麻烦。今天看到一个不需要其他任何外部动态链接库的java 包,特此记录下
https://github.com/oshi/oshi
该项目需要引入
jna、jna-platform jar ,直接加在 pom文件中就可以。
API文档
一段 参考代码:
@Slf4j @Service public class SystemService { public String getComputerIdentifier() { SystemInfo systemInfo = new SystemInfo(); LinuxDisks disks=new LinuxDisks(); OperatingSystem operatingSystem = systemInfo.getOperatingSystem(); HardwareAbstractionLayer hardwareAbstractionLayer = systemInfo.getHardware(); CentralProcessor centralProcessor = hardwareAbstractionLayer.getProcessor(); ComputerSystem computerSystem = hardwareAbstractionLayer.getComputerSystem(); String vendor = operatingSystem.getManufacturer(); String processorSerialNumber = computerSystem.getSerialNumber(); String processorIdentifier = centralProcessor.getProcessorIdentifier().getIdentifier(); int processors = centralProcessor.getLogicalProcessorCount(); String delimiter = "-"; HWDiskStore [] ha= disks.getDisks(); Arrays.stream(ha).forEach(a-> Arrays.stream(a.getPartitions()).forEach(hwPartition -> System.out.println( hwPartition.getName()))); return String.format("%08x", vendor.hashCode()) + delimiter + String.format("%08x", processorSerialNumber.hashCode()) + delimiter + String.format("%08x", processorIdentifier.hashCode()) + delimiter + processors; } public void logSysInfo(){ log.info(getComputerIdentifier()); } }