「java读取cpu序列号」java获取cpu型号

博主:adminadmin 2022-11-24 12:18:07 137

今天给各位分享java读取cpu序列号的知识,其中也会对java获取cpu型号进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

跪求在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;

}

}

}

试试是否可以:)

怎样用java 获取 硬盘 cpu 序列号,可调用dll实现

我是通过一个外部的JAR包来间接来获得DLL文件的句柄 的,它就是jacob了,这是java com brige的简写,呵呵, 这个名称起得非常形象吧,我用的版本是jacob 1.9的,你可以到它的官方网站去下载,下载回来的压缩包中会有两个文件我们需要用到的,一个是jacob.dll,一个是jacob.jar,jacob.dll可以将它复制到系统的system32目录下,而jacob.jar文件,直接将它加入到项目的库中就可以了。这两项准备工作完成后,就可以开始尝试调用了。

新建一个类,引入jacob.jar中的两个类,

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

然后通过dll的ControllerId来读取DLL文件

public class Print {

private ActiveXComponent printController = null;

private Dispatch printObj = null;/*** 默认controllerId的方法*/public Print(){try{printController = new ActiveXComponent(POSControler.Controler);

printObj = (Dispatch)printController.getObject();

}catch(Exception e){

printObj = new Dispatch();

如果方法dll中的方法是空参数时,直接call一下就可以了,如

Dispatch.call(printObj,setDefaultFont);而调用有参数的方法时,则需要将参数在后面依次传入,注意按顺序噢:

java如何获取本机主板序列号?

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();

}

System.out.println(result);

return result.trim();

}

public static void main(String[] args)

{

getMotherboardSN();

}

这个是我在网上找的,但是只能在windows下获得主板序列号,在linux下就不行。我愁~在linux下如何获得主板序列号呢。

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());

}

}

java读取cpu序列号的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java获取cpu型号、java读取cpu序列号的信息别忘了在本站进行查找喔。

The End

发布于:2022-11-24,除非注明,否则均为首码项目网原创文章,转载请注明出处。