Sigar(全称System Information Gatherer And Reporter,即系统信息收集报表器),它提供了一个开源的跨平台的收集计算机硬件和操作系统信息的API(该API底层接口用C语言编写),本文将演示如何借助Sigar API获取操作系统信息:
package com.ghj.packageoftest; import java.net.InetAddress; import org.hyperic.sigar.OperatingSystem; import org.hyperic.sigar.Sigar; import org.hyperic.sigar.SigarException; import org.hyperic.sigar.Who; /** * 借助Sigar API获取操作系统信息 * * @author GaoHuanjie */ public class OSTool { public static void main(String[] args) throws SigarException { Sigar sigar = new Sigar(); // 取到当前操作系统的名称 String hostname = ""; try { hostname = InetAddress.getLocalHost().getHostName(); } catch (Exception e) { hostname = sigar.getNetInfo().getHostName(); } System.out.println(hostname); // 获取当前操作系统的信息 OperatingSystem operatingSystem = OperatingSystem.getInstance(); System.out.println("operatingSystem arch:" + operatingSystem.getArch());// 操作系统内核类型如: 386、486、586等x86 System.out.println("operatingSystem cpuEndian:" + operatingSystem.getCpuEndian());// System.out.println("operatingSystem dataModel:" + operatingSystem.getDataModel());// System.out.println("operatingSystem description:" + operatingSystem.getDescription());// 系统描述 System.out.println("operatingSystem machine:" + operatingSystem.getMachine());// System.out.println("operatingSystem name:" + operatingSystem.getName());// 操作系统类型 System.out.println("operatingSystem patchLevel:" + operatingSystem.getPatchLevel());// 操作系统补丁级别 System.out.println("operatingSystem vendor:" + operatingSystem.getVendor());// 操作系统供应商 System.out.println("operatingSystem vendorCodeName:" + operatingSystem.getVendorCodeName());// 供应商编码名 System.out.println("operatingSystem vendorName:" + operatingSystem.getVendorName());// 操作系统供应商名称 System.out.println("operatingSystem vendorVersion:" + operatingSystem.getVendorVersion());// 操作系统供应商版本 System.out.println("operatingSystem version:" + operatingSystem.getVersion());// 操作系统的版本号 // 获取当前系统进程表中的用户信息 Who whoArray [] = sigar.getWhoList(); if (whoArray != null) { for (int i = 0; i < whoArray.length; i++) { Who who = whoArray[i]; System.out.println("\n~~~~~~~~~" + i + "~~~~~~~~~~~~"); System.out.println("who device:" + who.getDevice()); System.out.println("who host:" + who.getHost()); System.out.println("who time:" + who.getTime()); System.out.println("who user:" + who.getUser());// 当前系统进程表中的用户名 } } sigar.close(); } }
【0分下载示例工程】
时间: 2024-10-23 23:54:09