java sigar获取本地信息以及org.hyperic.sigar.SigarException: The device is not ready报错解决
window 下,使用 java sigar 获取磁盘使用率,cpu 使用率以及内存使用情况等信息时。
一:首先需要下载 jar 包和相关文件
如果想了解更多可以去
二:将压缩包解压,然后复制目录下的 hyperic-sigar-1.6.4\sigar-bin\lib\sigar-amd64-winnt.dll 文件。
注意:根据自己的电脑选择正确的 sigar.dll 文件,我的电脑是 64 位的。
三:将 sigar-amd64-winnt.dll 文件放在你本机 jdk 的 bin 目录下(如:我本机 jdk 的 bin 目录为 E:\JAVA\jdk\jdk1.8.0_25\bin)。
四:将解压后目录下的 hyperic-sigar-1.6.4\sigar-bin\lib\sigar.jar 包,导入到你的项目中。
若是 maven 项目,那么配置如下引入
<dependency>
<groupId>org.fusesource</groupId>
<artifactId>sigar</artifactId>
<version>1.6.4</version>
</dependency>
就会自动下载了。
五:最后复制下面这段代码:
package com.example.demo;import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;
import java.util.Properties;
import org.hyperic.sigar.CpuInfo;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.FileSystem;
import org.hyperic.sigar.FileSystemUsage;
import org.hyperic.sigar.Mem;
import org.hyperic.sigar.NetFlags;
import org.hyperic.sigar.NetInterfaceConfig;
import org.hyperic.sigar.NetInterfaceStat;
import org.hyperic.sigar.OperatingSystem;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.Swap;
import org.hyperic.sigar.Who;public class Test {
public static void main(String[] args) {
try {
// System 信息,从 jvm 获取
property();
System.out.println("----------------------------------");
// cpu 信息
cpu();
System.out.println("----------------------------------");
// 内存信息
memory();
System.out.println("----------------------------------");
// 操作系统信息
os();
System.out.println("----------------------------------");
// 用户信息
who();
System.out.println("----------------------------------");
// 文件系统信息
file();
System.out.println("----------------------------------");
// 网络信息
net();
System.out.println("----------------------------------");
// 以太网信息
ethernet();
System.out.println("----------------------------------");
} catch (Exception e1) {
e1.printStackTrace();
}
}</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> property() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> UnknownHostException { Runtime r </span>=<span style="color: rgba(0, 0, 0, 1)"> Runtime.getRuntime(); Properties props </span>=<span style="color: rgba(0, 0, 0, 1)"> System.getProperties(); InetAddress addr; addr </span>=<span style="color: rgba(0, 0, 0, 1)"> InetAddress.getLocalHost(); String ip </span>=<span style="color: rgba(0, 0, 0, 1)"> addr.getHostAddress(); Map</span><String, String> map =<span style="color: rgba(0, 0, 0, 1)"> System.getenv(); String userName </span>= map.get("USERNAME");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取用户名</span> String computerName = map.get("COMPUTERNAME");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取计算机名</span> String userDomain = map.get("USERDOMAIN");<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获取计算机域名</span> System.out.println("用户名: " +<span style="color: rgba(0, 0, 0, 1)"> userName); System.out.println(</span>"计算机名: " +<span style="color: rgba(0, 0, 0, 1)"> computerName); System.out.println(</span>"计算机域名: " +<span style="color: rgba(0, 0, 0, 1)"> userDomain); System.out.println(</span>"本地ip地址: " +<span style="color: rgba(0, 0, 0, 1)"> ip); System.out.println(</span>"本地主机名: " +<span style="color: rgba(0, 0, 0, 1)"> addr.getHostName()); System.out.println(</span>"JVM可以使用的总内存: " +<span style="color: rgba(0, 0, 0, 1)"> r.totalMemory()); System.out.println(</span>"JVM可以使用的剩余内存: " +<span style="color: rgba(0, 0, 0, 1)"> r.freeMemory()); System.out.println(</span>"JVM可以使用的处理器个数: " +<span style="color: rgba(0, 0, 0, 1)"> r.availableProcessors()); System.out.println(</span>"Java的运行环境版本: " + props.getProperty("java.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的运行环境供应商: " + props.getProperty("java.vendor"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java供应商的URL: " + props.getProperty("java.vendor.url"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的安装路径: " + props.getProperty("java.home"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机规范版本: " + props.getProperty("java.vm.specification.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机规范供应商: " + props.getProperty("java.vm.specification.vendor"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机规范名称: " + props.getProperty("java.vm.specification.name"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机实现版本: " + props.getProperty("java.vm.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机实现供应商: " + props.getProperty("java.vm.vendor"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的虚拟机实现名称: " + props.getProperty("java.vm.name"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java运行时环境规范版本: " + props.getProperty("java.specification.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java运行时环境规范供应商: " + props.getProperty("java.specification.vender"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java运行时环境规范名称: " + props.getProperty("java.specification.name"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的类格式版本号: " + props.getProperty("java.class.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"Java的类路径: " + props.getProperty("java.class.path"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"加载库时搜索的路径列表: " + props.getProperty("java.library.path"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"默认的临时文件路径: " + props.getProperty("java.io.tmpdir"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"一个或多个扩展目录的路径: " + props.getProperty("java.ext.dirs"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"操作系统的名称: " + props.getProperty("os.name"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"操作系统的构架: " + props.getProperty("os.arch"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"操作系统的版本: " + props.getProperty("os.version"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"文件分隔符: " + props.getProperty("file.separator"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"路径分隔符: " + props.getProperty("path.separator"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"行分隔符: " + props.getProperty("line.separator"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"用户的账户名称: " + props.getProperty("user.name"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"用户的主目录: " + props.getProperty("user.home"<span style="color: rgba(0, 0, 0, 1)">)); System.out.println(</span>"用户的当前工作目录: " + props.getProperty("user.dir"<span style="color: rgba(0, 0, 0, 1)">)); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> memory() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SigarException { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); Mem mem </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getMem(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 内存总量</span> System.out.println("内存总量: " + mem.getTotal() / 1024L + "K av"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前内存使用量</span> System.out.println("当前内存使用量: " + mem.getUsed() / 1024L + "K used"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前内存剩余量</span> System.out.println("当前内存剩余量: " + mem.getFree() / 1024L + "K free"<span style="color: rgba(0, 0, 0, 1)">); Swap swap </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getSwap(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 交换区总量</span> System.out.println("交换区总量: " + swap.getTotal() / 1024L + "K av"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前交换区使用量</span> System.out.println("当前交换区使用量: " + swap.getUsed() / 1024L + "K used"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前交换区剩余量</span> System.out.println("当前交换区剩余量: " + swap.getFree() / 1024L + "K free"<span style="color: rgba(0, 0, 0, 1)">); } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> cpu() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SigarException { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); CpuInfo infos[] </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getCpuInfoList(); CpuPerc cpuList[] </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; cpuList </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getCpuPercList(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < infos.length; i++) {<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 不管是单块CPU还是多CPU都适用</span> CpuInfo info =<span style="color: rgba(0, 0, 0, 1)"> infos[i]; System.out.println(</span>"第" + (i + 1) + "块CPU信息"<span style="color: rgba(0, 0, 0, 1)">); System.out.println(</span>"CPU的总量MHz: " + info.getMhz());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> CPU的总量MHz</span> System.out.println("CPU生产商: " + info.getVendor());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得CPU的卖主,如:Intel</span> System.out.println("CPU类别: " + info.getModel());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 获得CPU的类别,如:Celeron</span> System.out.println("CPU缓存数量: " + info.getCacheSize());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 缓冲存储器数量</span>
printCpuPerc(cpuList[i]);
}
}</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> printCpuPerc(CpuPerc cpu) { System.out.println(</span>"CPU用户使用率: " + CpuPerc.format(cpu.getUser()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 用户使用率</span> System.out.println("CPU系统使用率: " + CpuPerc.format(cpu.getSys()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 系统使用率</span> System.out.println("CPU当前等待率: " + CpuPerc.format(cpu.getWait()));<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前等待率</span> System.out.println("CPU当前错误率: " + CpuPerc.format(cpu.getNice()));<span style="color: rgba(0, 128, 0, 1)">//
System.out.println("CPU 当前空闲率:" + CpuPerc.format(cpu.getIdle()));// 当前空闲率
System.out.println("CPU 总的使用率:" + CpuPerc.format(cpu.getCombined()));// 总的使用率
}</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span><span style="color: rgba(0, 0, 0, 1)"> os() { OperatingSystem OS </span>=<span style="color: rgba(0, 0, 0, 1)"> OperatingSystem.getInstance(); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 操作系统内核类型如: 386、486、586等x86</span> System.out.println("操作系统: " +<span style="color: rgba(0, 0, 0, 1)"> OS.getArch()); System.out.println(</span>"操作系统CpuEndian(): " + OS.getCpuEndian());<span style="color: rgba(0, 128, 0, 1)">//
System.out.println("操作系统 DataModel():" + OS.getDataModel());//
// 系统描述
System.out.println("操作系统的描述:" + OS.getDescription());
// 操作系统类型
// System.out.println("OS.getName():" + OS.getName());
// System.out.println("OS.getPatchLevel():" + OS.getPatchLevel());//
// 操作系统的卖主
System.out.println("操作系统的卖主:" + OS.getVendor());
// 卖主名称
System.out.println("操作系统的卖主名:" + OS.getVendorCodeName());
// 操作系统名称
System.out.println("操作系统名称:" + OS.getVendorName());
// 操作系统卖主类型
System.out.println("操作系统卖主类型:" + OS.getVendorVersion());
// 操作系统的版本号
System.out.println("操作系统的版本号:" + OS.getVersion());
}</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> who() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SigarException { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); Who who[] </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getWhoList(); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (who != <span style="color: rgba(0, 0, 255, 1)">null</span> && who.length > 0<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < who.length; i++<span style="color: rgba(0, 0, 0, 1)">) { </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println("当前系统进程表中的用户名" + String.valueOf(i));</span> Who _who =<span style="color: rgba(0, 0, 0, 1)"> who[i]; System.out.println(</span>"用户控制台: " +<span style="color: rgba(0, 0, 0, 1)"> _who.getDevice()); System.out.println(</span>"用户host: " +<span style="color: rgba(0, 0, 0, 1)"> _who.getHost()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> System.out.println("getTime(): " + _who.getTime()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 当前系统进程表中的用户名</span> System.out.println("当前系统进程表中的用户名: " +<span style="color: rgba(0, 0, 0, 1)"> _who.getUser()); } } } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> file() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); FileSystem fslist[] </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getFileSystemList(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < fslist.length; i++<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(</span>"分区的盘符名称" +<span style="color: rgba(0, 0, 0, 1)"> i); FileSystem fs </span>=<span style="color: rgba(0, 0, 0, 1)"> fslist[i]; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分区的盘符名称</span> System.out.println("盘符名称: " +<span style="color: rgba(0, 0, 0, 1)"> fs.getDevName()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 分区的盘符名称</span> System.out.println("盘符路径: " +<span style="color: rgba(0, 0, 0, 1)"> fs.getDirName()); System.out.println(</span>"盘符标志: " + fs.getFlags());<span style="color: rgba(0, 128, 0, 1)">//</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统类型,比如 FAT32、NTFS</span> System.out.println("盘符类型: " +<span style="color: rgba(0, 0, 0, 1)"> fs.getSysTypeName()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统类型名,比如本地硬盘、光驱、网络文件系统等</span> System.out.println("盘符类型名: " +<span style="color: rgba(0, 0, 0, 1)"> fs.getTypeName()); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统类型</span> System.out.println("盘符文件系统类型: " +<span style="color: rgba(0, 0, 0, 1)"> fs.getType()); FileSystemUsage usage </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; usage </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getFileSystemUsage(fs.getDirName()); </span><span style="color: rgba(0, 0, 255, 1)">switch</span><span style="color: rgba(0, 0, 0, 1)"> (fs.getType()) { </span><span style="color: rgba(0, 0, 255, 1)">case</span> 0: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_UNKNOWN :未知</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 1: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_NONE</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 2: <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_LOCAL_DISK : 本地硬盘 </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统总大小</span> System.out.println(fs.getDevName() + "总大小: " + usage.getTotal() + "KB"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统剩余大小</span> System.out.println(fs.getDevName() + "剩余大小: " + usage.getFree() + "KB"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统可用大小</span> System.out.println(fs.getDevName() + "可用大小: " + usage.getAvail() + "KB"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统已经使用量</span> System.out.println(fs.getDevName() + "已经使用量: " + usage.getUsed() + "KB"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">double</span> usePercent = usage.getUsePercent() *<span style="color: rgba(0, 0, 0, 1)"> 100D; </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 文件系统资源的利用率</span> System.out.println(fs.getDevName() + "资源的利用率: " + usePercent + "%"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 3:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_NETWORK :网络</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 4:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_RAM_DISK :闪存</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 5:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_CDROM :光驱</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; </span><span style="color: rgba(0, 0, 255, 1)">case</span> 6:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> TYPE_SWAP :页面交换</span> <span style="color: rgba(0, 0, 255, 1)">break</span><span style="color: rgba(0, 0, 0, 1)">; } System.out.println(fs.getDevName() </span>+ "读出: " +<span style="color: rgba(0, 0, 0, 1)"> usage.getDiskReads()); System.out.println(fs.getDevName() </span>+ "写入: " +<span style="color: rgba(0, 0, 0, 1)"> usage.getDiskWrites()); } </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)">; } </span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> net() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> Exception { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); String ifNames[] </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getNetInterfaceList(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < ifNames.length; i++<span style="color: rgba(0, 0, 0, 1)">) { String name </span>=<span style="color: rgba(0, 0, 0, 1)"> ifNames[i]; NetInterfaceConfig ifconfig </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getNetInterfaceConfig(name); System.out.println(</span>"网络设备名: " + name);<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 网络设备名</span> System.out.println("IP地址: " + ifconfig.getAddress());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> IP地址</span> System.out.println("子网掩码: " + ifconfig.getNetmask());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 子网掩码</span> <span style="color: rgba(0, 0, 255, 1)">if</span> ((ifconfig.getFlags() & 1L) <= 0L<span style="color: rgba(0, 0, 0, 1)">) { System.out.println(</span>"!IFF_UP...skipping getNetInterfaceStat"<span style="color: rgba(0, 0, 0, 1)">); </span><span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">; } NetInterfaceStat ifstat </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getNetInterfaceStat(name); System.out.println(name </span>+ "接收的总包裹数:" + ifstat.getRxPackets());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收的总包裹数</span> System.out.println(name + "发送的总包裹数:" + ifstat.getTxPackets());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送的总包裹数</span> System.out.println(name + "接收到的总字节数:" + ifstat.getRxBytes());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收到的总字节数</span> System.out.println(name + "发送的总字节数:" + ifstat.getTxBytes());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送的总字节数</span> System.out.println(name + "接收到的错误包数:" + ifstat.getRxErrors());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收到的错误包数</span> System.out.println(name + "发送数据包时的错误数:" + ifstat.getTxErrors());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送数据包时的错误数</span> System.out.println(name + "接收时丢弃的包数:" + ifstat.getRxDropped());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 接收时丢弃的包数</span> System.out.println(name + "发送时丢弃的包数:" + ifstat.getTxDropped());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 发送时丢弃的包数</span>
}
}</span><span style="color: rgba(0, 0, 255, 1)">private</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">void</span> ethernet() <span style="color: rgba(0, 0, 255, 1)">throws</span><span style="color: rgba(0, 0, 0, 1)"> SigarException { Sigar sigar </span>= <span style="color: rgba(0, 0, 255, 1)">null</span><span style="color: rgba(0, 0, 0, 1)">; sigar </span>= <span style="color: rgba(0, 0, 255, 1)">new</span><span style="color: rgba(0, 0, 0, 1)"> Sigar(); String[] ifaces </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getNetInterfaceList(); </span><span style="color: rgba(0, 0, 255, 1)">for</span> (<span style="color: rgba(0, 0, 255, 1)">int</span> i = 0; i < ifaces.length; i++<span style="color: rgba(0, 0, 0, 1)">) { NetInterfaceConfig cfg </span>=<span style="color: rgba(0, 0, 0, 1)"> sigar.getNetInterfaceConfig(ifaces[i]); </span><span style="color: rgba(0, 0, 255, 1)">if</span> (NetFlags.LOOPBACK_ADDRESS.equals(cfg.getAddress()) || (cfg.getFlags() & NetFlags.IFF_LOOPBACK) != 0 ||<span style="color: rgba(0, 0, 0, 1)"> NetFlags.NULL_HWADDR.equals(cfg.getHwaddr())) { </span><span style="color: rgba(0, 0, 255, 1)">continue</span><span style="color: rgba(0, 0, 0, 1)">; } System.out.println(cfg.getName() </span>+ "IP地址:" + cfg.getAddress());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> IP地址</span> System.out.println(cfg.getName() + "网关广播地址:" + cfg.getBroadcast());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 网关广播地址</span> System.out.println(cfg.getName() + "网卡MAC地址:" + cfg.getHwaddr());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 网卡MAC地址</span> System.out.println(cfg.getName() + "子网掩码:" + cfg.getNetmask());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 子网掩码</span> System.out.println(cfg.getName() + "网卡描述信息:" + cfg.getDescription());<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> 网卡描述信息</span> System.out.println(cfg.getName() + "网卡类型" + cfg.getType());<span style="color: rgba(0, 128, 0, 1)">//
}
}
}
到这里就可以获取你电脑相应的信息。
别急还没完!!!
这里会报错,这行代码:usage = sigar.getFileSystemUsage(fs.getDirName());。

我们需要将上面这行代码用 try catch surround 一下问题就解决了,如下:
try { usage = sigar.getFileSystemUsage(fs.getDirName()); } catch (SigarException e) { System.out.println("***********"); continue; }