随手记录
一、引入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: " + 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(" serialnu