导航:首页 > 编程语言 > java获取cpu编号

java获取cpu编号

发布时间:2025-09-17 07:33:54

A. java 能不能获取CPU的ID号,硬盘的序列号

能,
import java.io.BufferedReader;
import java.io.File;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class HardWareUtils {

/**
* 获取主板序列号
*
* @return
*/
public static String getMotherboardSN() {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_BaseBoard\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.SerialNumber \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";

fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}

/**
* 获取硬盘序列号
*
* @param drive
* 盘符
* @return
*/
public static String getHardDiskSN(String drive) {
String result = "";
try {
File file = File.createTempFile("realhowto", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);

String vbs = "Set objFSO = CreateObject(\"Scripting.FileSystemObject\")\n"
+ "Set colDrives = objFSO.Drives\n"
+ "Set objDrive = colDrives.item(\""
+ drive
+ "\")\n"
+ "Wscript.Echo objDrive.SerialNumber"; // see note
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
} catch (Exception e) {
e.printStackTrace();
}
return result.trim();
}

/**
* 获取CPU序列号
*
* @return
*/
public static String getCPUSerial() {
String result = "";
try {
File file = File.createTempFile("tmp", ".vbs");
file.deleteOnExit();
FileWriter fw = new java.io.FileWriter(file);
String vbs = "Set objWMIService = GetObject(\"winmgmts:\\\\.\\root\\cimv2\")\n"
+ "Set colItems = objWMIService.ExecQuery _ \n"
+ " (\"Select * from Win32_Processor\") \n"
+ "For Each objItem in colItems \n"
+ " Wscript.Echo objItem.ProcessorId \n"
+ " exit for ' do the first cpu only! \n" + "Next \n";

// + " exit for \r\n" + "Next";
fw.write(vbs);
fw.close();
Process p = Runtime.getRuntime().exec(
"cscript //NoLogo " + file.getPath());
BufferedReader input = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String line;
while ((line = input.readLine()) != null) {
result += line;
}
input.close();
file.delete();
} catch (Exception e) {
e.fillInStackTrace();
}
if (result.trim().length() < 1 || result == null) {
result = "无CPU_ID被读取";
}
return result.trim();
}

/**
* 获取MAC地址
*/
public static String getMac(){
String result = "";
try {

Process process = Runtime.getRuntime().exec("ipconfig /all");

InputStreamReader ir = new InputStreamReader(process.getInputStream());

LineNumberReader input = new LineNumberReader(ir);

String line;

while ((line = input.readLine()) != null)

if (line.indexOf("Physical Address") > 0) {

String MACAddr = line.substring(line.indexOf("-") - 2);

result=MACAddr;

}

} catch (java.io.IOException e) {

System.err.println("IOException " + e.getMessage());

}
return result;
}

public static void main(String[] args) {
System.out.println("CPU SN:"+HardWareUtils.getCPUSerial());
System.out.println("主板 SN:"+HardWareUtils.getMotherboardSN());
System.out.println("C盘 SN:"+HardWareUtils.getHardDiskSN("c"));
System.out.println("MAC SN:"+HardWareUtils.getMac());
}

}

B. 跪求在JAVA里如何获得CPU的序列号,和硬盘的序列号。

利用Runtime call操作系统的命令,具体的命令取决于不同的操作系统,注意不要调用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])这个接口,不然复杂命令的执行会有问题。例子如下(拿cpu个数,其他类似):
定义命令:
WindowsCmd ="cmd.exe /c echo %NUMBER_OF_PROCESSORS%";//windows的特殊
SolarisCmd = {"/bin/sh", "-c", "/usr/sbin/psrinfo | wc -l"};
AIXCmd = {"/bin/sh", "-c", "/usr/sbin/lsdev -Cc processor | wc -l"};
HPUXCmd = {"/bin/sh", "-c", "echo \"map\" | /usr/sbin/cstm | grep CPU | wc -l "};
LinuxCmd = {"/bin/sh", "-c", "cat /proc/cpuinfo | grep ^process | wc -l"};

然后判断系统:
os = System.getProperty("os.name").toLowerCase();

根据不同的操作系统call不同的命令。
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class GetMACAddress
{
public String getMACAddress(String ipAddress)
{
String str = "",strMAC = "",macAddress = "";
try
{
Process pp = Runtime.getRuntime().exec("nbtstat -a " + ipAddress);
InputStreamReader ir = new InputStreamReader(pp.getInputStream());
LineNumberReader input = new LineNumberReader(ir);
for(int i = 1;i < 100;i++)
{
str = input.readLine();
if(str != null)
{
if(str.indexOf("MAC Address") > 1)
{
strMAC = str.substring(str.indexOf("MAC Address") + 14,str.length());
break;
}
}
}
}
catch(IOException ex)
{
return "Can't Get MAC Address!";
}
//
if(strMAC.length() < 17)
{
return "Error!";
}
macAddress = strMAC.substring(0,2) + ":"
+ strMAC.substring(3,5) + ":"
+ strMAC.substring(6,8) + ":"
+ strMAC.substring(9,11) + ":"
+ strMAC.substring(12,14) + ":"
+ strMAC.substring(15,17);
//
return macAddress;
}

public static void main(String[] args)
{
GetMACAddress getMACAddress = new GetMACAddress();
System.out.println(getMACAddress.getMACAddress("172.18.8.225"));

try
{
java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
InputStream istr = proc.getInputStream();
byte[] data = new byte[1024];
istr.read(data);
String netdata = new String(data);
System.out.println("Your Mac Address=" + procAll(netdata));
}
catch(IOException e)
{
System.out.println("error=" + e);
}
}

public static String procAll(String str)
{
return procStringEnd(procFirstMac(procAddress(str)));
}

public static String procAddress(String str)
{
int indexof = str.indexOf("Physical Address");
if(indexof > 0)
{
return str.substring(indexof,str.length());
}
return str;
}

public static String procFirstMac(String str)
{
int indexof = str.indexOf(":");
if(indexof > 0)
{
return str.substring(indexof + 1,str.length()).trim();
}
return str;
}

public static String procStringEnd(String str)
{
int indexof = str.indexOf("\r");
if(indexof > 0)
{
return str.substring(0,indexof).trim();
}
return str;
}
}

import java.util.Vector;

class GetNetMAC
{
//网卡物理地址长度
static private final int _physicalLength = 16;

public static void main(String[] args)
{
//output you computer phycail ip address
System.out.println("The MAC Addressis:\t" + getPhysicalAddress());
}

static public String getPhysicalAddress()
{
GetNetMACShell shell = new GetNetMACShell();
String cmd = "cmd.exe /c ipconfig/all";
Vector result;
result = shell.execute(cmd);
return parseCmd(result.toString());
}

//从字符串中解析出所需要获得的字符串
static private String parseCmd(String s)
{
String find = "Physical Address. . . . . . . . . :";
int findIndex = s.indexOf(find);
if(findIndex == -1)
{
return "not find";
}
else
{
return s.substring(findIndex + find.length() + 1,findIndex + find.length() + 1 + _physicalLength);
}
}
}

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.InputStreamReader;
import java.util.Vector;

public class GetNetMACShell
{
private Process process = null;

public Vector execute(String shellCommand)
{
try
{
Start(shellCommand);
Vector vResult = new Vector();
DataInputStream in = new DataInputStream(process.getInputStream());
BufferedReader reader = new BufferedReader(new InputStreamReader(in));

String line;
do
{
line = reader.readLine();
if(line == null)
{
break;
}
else
{
vResult.addElement(line);
}
}
while(true);
reader.close();
return vResult;

}
catch(Exception e)
{
//error
return null;
}
}

public void Start(String shellCommand)
{
try
{
if(process != null)
{
kill();
}
Runtime sys = Runtime.getRuntime();
process = sys.exec(shellCommand);
}
catch(Exception e)
{
System.out.println(e.toString());
}
}

public void kill()
{
if(process != null)
{
process.destroy();
process = null;
}
}
}

试试是否可以:)

C. java 如何获得一个进程的内存使用情况,cpu运行的时间

首先有个基本问题需要了解一下:
这里所说java里获得一个进程的内存使用情况和cpu运行时间,是指在java内部获取一个纯外部进程的内存与cpu时间呢,还是指在java内部,由java启动的进程的内存与cpu时间。

如果是第一种情况,那你还需要在java内部再起一个进程,通过执行操作系统的shell命令来查看那个进程的运行状态。比如那个外部进程的ID为3119,则执行cat /proc/3119/status | grep VmRSS就可以过滤出该进程的物理内存占用量。

如果是第二种情况,(假定你问的就是这种情况)。
先说内存占用量:一般说来,你可以使用这两种方式获取内存使用情况
方式一:
MemoryMXBean memoryMXBean = ManagementFactory.getMemoryMXBean();
MemoryUsage memoryUsage = memoryMXBean.getHeapMemoryUsage(); //椎内存使用情况
long totalMemorySize = memoryUsage.getInit(); //初始的总内存
long maxMemorySize = memoryUsage.getMax(); //最大可用内存
long usedMemorySize = memoryUsage.getUsed(); //已使用的内存

方式二:
Runtime rt = Runtime.getRuntime();
long totalMemorySize = rt.totalMemory(); //初始的总内存
long maxMemorySiz = t.maxMemory(); //最大可用内存
long freeMemorySize = rt.freeMemory(); //当前可用内存

需要说明的是,这种方式获取的是整个jvm的内存使用情况,并不是某一个进程的内存使用情况,事实上,在java内部,可以使用Rumtime.getRuntime().exec(${SHELL})来开启一个外部进程(这里${SHELL}代表一个可操作系统的shell命令)。而运行Java程序整个jvm,对于操作系统而言,也仅仅只是一个进程。也就是说,一个jvm就是一个进程,你通过java程序开启的进程都是外部进程,java内部目前还提供了一个destroy方法来销毁该进程,对于该进程的其它信息,都无法直接获取,这些信息的获取,显然需要本地化(Local)的实现。既然标准jdk库没有,就不可能再通过平台无关的代码来实现了。典型的做法就是使用前面第一种情况的方式,再启一个进程,执行shell命令来获取。

不过对于cpu使用时间,采用标准java代码倒是可以拿到。由于java的语法很啰嗦,举一个较完全的例子需要太多的代码,我这里就只写最关键的代码:
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
① long currentCpuTime = threadMXBean.getCurrentThreadCpuTime(); //当前线程的cpu使用时间
long someThreadId = 709817L; //假定有某个线程的ID是709817
② long someThreadCpuTime = threadMXBean.getThreadCpuTime(someThreadId); //获取ID为someThreadId即709817的线程的cpu时间

基于上面的核心api,你可以把由java启动的外部进程放到一个单独的线程中执行,再用代码②的方式来获取该进程的cpu使用时间,也可以将外部进程放入到当前线程中执行,用① 的方式来获得进程的cpu使用时间。

阅读全文

与java获取cpu编号相关的资料

热点内容
如何在c51中调用汇编程序 浏览:824
java字符串转码gbk 浏览:983
win怎么打开隐藏文件夹选项 浏览:454
荣耀手机qq接收文件在哪个文件夹 浏览:708
messagebox的头文件 浏览:100
java启动线程处理 浏览:622
腾迅收藏的表情在哪个文件夹 浏览:388
为什么excel的文件是word打开的 浏览:781
红头文件字体格式word 浏览:443
iis字体配置文件 浏览:285
少儿编程导师是做什么的 浏览:593
当前的网络安全防御技术有哪些 浏览:655
win如何重置用户配置文件 浏览:178
java获取cpu编号 浏览:960
qt文件指针移动 浏览:655
苹果4s耳机插孔失灵 浏览:923
qq漫游记录在手机哪个文件夹 浏览:58
设置hdd密码 浏览:624
iphone4s升级ios9失败怎么办 浏览:6
ro装备有洞升级后 浏览:958

友情链接