一、引入jar包
本项目主要使用第开源jar包:https://github.com/oshi/oshi
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> <!-- https://mvnrepository.com/artifact/com.github.oshi/oshi-core --> <dependency> <groupId>com.github.oshi</groupId> <artifactId>oshi-core</artifactId> <version>3.5.0</version> </dependency>
二、测试代码
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; /** * The Class SystemInfoTest. * * @author dblock[at]dblock[dot]org */ public class SystemInfoTest { /** * The main method. * * @param args the arguments */ public static void main(String[] args) { // Options: ERROR > WARN > INFO > DEBUG > TRACE Logger LOG = LoggerFactory.getLogger(SystemInfoTest.class); LOG.info("Initializing System..."); SystemInfo si = new SystemInfo(); HardwareAbstractionLayer hal = si.getHardware(); OperatingSystem os = si.getOperatingSystem(); System.out.println(os); LOG.info("Checking computer system..."); printComputerSystem(hal.getComputerSystem()); LOG.info("Checking Processor..."); printProcessor(hal.getProcessor()); LOG.info("Checking Memory..."); printMemory(hal.getMemory()); LOG.info("Checking CPU..."); printCpu(hal.getProcessor()); LOG.info("Checking Processes..."); printProcesses(os, hal.getMemory()); LOG.info("Checking Sensors..."); printSensors(hal.getSensors()); LOG.info("Checking Power sources..."); printPowerSources(hal.getPowerSources()); LOG.info("Checking Disks..."); printDisks(hal.getDiskStores()); LOG.info("Checking File System..."); printFileSystem(os.getFileSystem()); LOG.info("Checking Network interfaces..."); printNetworkInterfaces(hal.getNetworkIFs()); LOG.info("Checking Network parameterss..."); printNetworkParameters(os.getNetworkParams()); // hardware: displays LOG.info("Checking Displays..."); printDisplays(hal.getDisplays()); // hardware: USB devices LOG.info("Checking USB Devices..."); printUsbDevices(hal.getUsbDevices(true)); } private static void printComputerSystem(final ComputerSystem computerSystem) { System.out.println("manufacturer: " + computerSystem.getManufacturer()); System.out.println("model: &