導航:首頁 > 編程語言 > 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

友情鏈接