java读取硬件信息
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
(原创)JA V A读取硬件信息(MAC地址,CPU号,硬盘卷标,CPU型号及CPU使用率等信息)
(2011-03-27 00:16:12)
转载
分类:java开发
标签:
java读mac地址
cpu号
硬盘卷标
cpu型号
cpu使用率等
杂谈
在发布新版的MYPM时,加密要用到相关硬件信息,于是写了下面的测试类
运行main 打印信息如下图
需要引用的包sigar-1.6.3.jar
及相关动态库测试时我加载了sigar自带所有动态库
详见main方法内
package mon.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import org.hyperic.sigar.CpuPerc;
import org.hyperic.sigar.Sigar;
import org.hyperic.sigar.SigarException;
import org.hyperic.sigar.SigarLoader;
import org.hyperic.sigar.cmd.Shell;
import org.hyperic.sigar.cmd.SigarCommandBase; public class CpuInfo extends SigarCommandBase { public boolean displayTimes = true;
public CpuInfo(Shell shell) {
super(shell);
}
public CpuInfo() {
super();
}
public String getUsageShort() {
return "Display cpu information";
}
private void output(CpuPerc cpu) {
println("User Time....." + CpuPerc.format(cpu.getUser()));
println("Sys Time......" + CpuPerc.format(cpu.getSys()));
println("Idle Time....." + CpuPerc.format(cpu.getIdle()));
println("Wait Time....." + CpuPerc.format(cpu.getWait()));
println("Nice Time....." + CpuPerc.format(cpu.getNice()));
println("Combined......" + CpuPerc.format(cpu.getCombined()));
println("Irq Time......" + CpuPerc.format(cpu.getIrq()));
if (SigarLoader.IS_LINUX) {
println("SoftIrq Time.." + CpuPerc.format(cpu.getSoftIrq()));
println("Stolen Time...." + CpuPerc.format(cpu.getStolen()));
}
println("");
}
public void output(String[] args) throws SigarException {
org.hyperic.sigar.CpuInfo[] infos = this.sigar.getCpuInfoList();
CpuPerc[] cpus = this.sigar.getCpuPercList();
org.hyperic.sigar.CpuInfo info = infos[0];
long cacheSize = info.getCacheSize();
println("Vendor........." + info.getVendor());
println("Model.........." + info.getModel());
println("Mhz............" + info.getMhz());
println("Total CPUs....." + info.getTotalCores());
if ((info.getTotalCores() != info.getTotalSockets()) || (info.getCoresPerSocket() > info.getTotalCores())) {
println("Physical CPUs.." + info.getTotalSockets());
println("Cores per CPU.." + info.getCoresPerSocket());
}
if (cacheSize != Sigar.FIELD_NOTIMPL) {
println("Cache size...." + cacheSize);
}
println("");
if (!this.displayTimes) {
return;
}
for (int i = 0; i < cpus.length; i++) {
println("CPU " + i + ".........");
output(cpus[i]);
}
println("Totals........");
output(this.sigar.getCpuPerc());
StringBuffer sb=new StringBuffer("cpu号="+getCPUSerial()+"\n");
String[] interfaces = sigar.getNetInterfaceList();
if(interfaces!=null || interfaces.length>0)
sb.append("第一个网卡号="+sigar.getNetInterfaceConfig(interfaces[0]).getHwaddr());
org.hyperic.sigar.FileSystem[] filesystems = sigar.getFileSystemList();
if(filesystems!=null || filesystems.length>0)
sb.append("\n"+"硬盘第一个分区的卷标="+getHDSerial(filesystems[1].getDevName()));
System.out.println(sb.toString());
}
public static void main(String[] args) throws Exception {
//先加载siga动太库在不同的平台只要加载特定的动态库,这里我就全加载不区分了//在IDE环境中,可以不加载动态库设置natinve lib patch location 既可
File nativeDir = new File("E:\\mypm10_new\\mypmdoc\\WebRoot\\WEB-INF\\native");
File[] libs = nativeDir.listFiles();
for (int i = 0; i < libs.length; i++) {
if (libs[i].isFile())
try {
System.load(new File(nativeDir, libs[i].getName()).getPath());
} catch (Throwable t) {
}
}
new CpuInfo().processCommand(args);
}
public static double getCpuIdle() {