利用Runtime call操作系統(tǒng)的命令,具體的命令取決于不同的操作系統(tǒng),注意不要調(diào)用Runtime.getRuntime().exec(String)接口,要用Runtime.getRuntime().exec(String[])這個(gè)接口,不然復(fù)雜命令的執(zhí)行會有問題。例子如下(拿cpu個(gè)數(shù),其他類似):
目前創(chuàng)新互聯(lián)已為上千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)頁空間、網(wǎng)站托管維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、莫力達(dá)網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
定義命令:
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"};
然后判斷系統(tǒng):
os = System.getProperty("os.name").toLowerCase();
根據(jù)不同的操作系統(tǒng)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
{
//網(wǎng)卡物理地址長度
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;
}
}
}
試試是否可以:)
我是通過一個(gè)外部的JAR包來間接來獲得DLL文件的句柄 的,它就是jacob了,這是java com brige的簡寫,呵呵, 這個(gè)名稱起得非常形象吧,我用的版本是jacob 1.9的,你可以到它的官方網(wǎng)站去下載,下載回來的壓縮包中會有兩個(gè)文件我們需要用到的,一個(gè)是jacob.dll,一個(gè)是jacob.jar,jacob.dll可以將它復(fù)制到系統(tǒng)的system32目錄下,而jacob.jar文件,直接將它加入到項(xiàng)目的庫中就可以了。這兩項(xiàng)準(zhǔn)備工作完成后,就可以開始嘗試調(diào)用了。
新建一個(gè)類,引入jacob.jar中的兩個(gè)類,
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
然后通過dll的ControllerId來讀取DLL文件
public class Print {
private ActiveXComponent printController = null;
private Dispatch printObj = null;/*** 默認(rèn)controllerId的方法*/public Print(){try{printController = new ActiveXComponent(POSControler.Controler);
printObj = (Dispatch)printController.getObject();
}catch(Exception e){
printObj = new Dispatch();
如果方法dll中的方法是空參數(shù)時(shí),直接call一下就可以了,如
Dispatch.call(printObj,setDefaultFont);而調(diào)用有參數(shù)的方法時(shí),則需要將參數(shù)在后面依次傳入,注意按順序噢:
一、上Google搜一下Mac及CPU ID的C代碼
二、上java.sun.com找那個(gè)JNI的指南
三、你用二的方法把一的代碼給包一下。
其實(shí)利用在java中的runtime來執(zhí)行在win里面的ipconfig -all的命令可以實(shí)現(xiàn)的??!
java.lang.Process proc = Runtime.getRuntime().exec("ipconfig /all");
InputStream istr = proc.getInputStream();
byte[] data = new byte[1024];
istr.read(data);
netdata = new String(data);
分析其中的netdata就是那!,有關(guān)cpu的可以類同實(shí)現(xiàn)!
1、在遠(yuǎn)程server里搭建一個(gè)http服務(wù),并支持某種服務(wù)端語言(如.net或php之類)
2、用服務(wù)端語言獲取到服務(wù)器的磁盤信息,把結(jié)果輸出
3、在本地用JS寫一段AJAX腳本,遠(yuǎn)程請求服務(wù)端寫好的程序,得到結(jié)果,并展示給用戶
html
head
title提取硬盤序列號/title
script
function disk() {//硬盤序列號 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_DiskDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.signature + "/td");
document.write("/tr");
}
document.write("/table");
}
/script
/head
body
input type="button" value="硬盤序列號" onclick="disk()"
/body
/html
在這上找的代碼:
對于寫過ASP或.net 的人來說,通過asp或.net 語言來獲得客戶端的硬件信息是很熟悉的.但如何通過javascript這種客戶端腳本來獲得客戶端的信息呢?請看以下實(shí)例:
!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"
html
head
titlejavascript獲得客戶端硬件信息-;/title
meta name="GENERATOR" content="Microsoft Visual Studio .NET
7.1"
meta name="vs_targetSchema"
content=""
script id=clientEventHandlersJS language=javascript
!--
function Button2_onclick() {//CPU 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Processor");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Caption + "/td");
document.write("td" + p.DeviceID + "/td");
document.write("td" + p.Name + "/td");
document.write("td" + p.CpuStatus + "/td");
document.write("td" + p.Availability + "/td");
document.write("td" + p.Level + "/td");
document.write("td" + p.ProcessorID + "/td");
document.write("td" + p.SystemName + "/td");
document.write("td" + p.ProcessorType + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//軟盤信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_FloppyDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Description + "/td");
document.write("td" + p.DeviceID + "/td");
document.write("td" + p.Status + "/td");
document.write("td" + p.Manufacuturer + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//CD-ROM 信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_CDROMDrive");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Caption + "/td");
document.write("td" + p.Description + "/td");
document.write("td" + p.Drive + "/td");
document.write("td" + p.Status + "/td");
document.write("td" + p.MediaLoaded + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//鍵盤信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_Keyboard");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.Description + "/td");
document.write("td" + p.Name + "/td");
document.write("td" + p.Status + "/td");
document.write("/tr");
}
document.write("/table");
}
function Button1_onclick() {//主板信息
var locator = new ActiveXObject ("WbemScripting.SWbemLocator");
var service = locator.ConnectServer(".");
var properties = service.ExecQuery("SELECT * FROM Win32_BaseBoard");
var e = new Enumerator (properties);
document.write("table border=1");
for (;!e.atEnd();e.moveNext ())
{
var p = e.item ();
document.write("tr");
document.write("td" + p.HostingBoard + "/td");
document.write("td" + p.Manufacturer + "/td");
document.write("td" + p.PoweredOn + "/td");
document.write("td" + p.Product + "/td");
document.write("td" + p.SerialNumber + "/td");
document.write("td" + p.Version + "/td");
document.write("/tr");
}
document.write("/table");
}
//--
/script
/head
body
INPUT id="Button1" type="button" value="Button"
name="Button1" language=javascript onclick="return Button1_onclick()"
/body
/html
使用javascript方式獲得客戶端的信息主要的優(yōu)點(diǎn)是,不需求服務(wù)器資源.不需求刷新網(wǎng)頁.直接在客戶端執(zhí)行腳本獲得顯示.
在這上找到了硬盤序列號的參數(shù):
\'獲得硬盤序列號
Dim cmicWmi As New System.Management.ManagementObjectSearcher(\"SELECT * FROM Win32_DiskDrive\")
Dim Uint32 As UInt32
For Each cmicWmiObj As ManagementObject In cmicWmi.Get
Uint32 = cmicWmiObj(\"signature\")
Next
TextBox1.Text = Uint32.ToString
和在一塊總算給提出來了,呵呵。
public?static?void?main(String[]?args)?{
String?HDserialnumber?=?getHdSerialInfo();
System.out.println(HDserialnumber);
}
public?static?String?getHdSerialInfo()?{
String?line?=?"";
String?HdSerial?=?"";//?定義變量?硬盤序列號
try?{
Process?proces?=?Runtime.getRuntime().exec("cmd?/c?dir?c:");//?獲取命令行參數(shù)
BufferedReader?buffreader?=?new?BufferedReader(new?InputStreamReader(proces.getInputStream()));
while?((line?=?buffreader.readLine())?!=?null)?{
if?(line.indexOf("卷的序列號是?")?!=?-1)?{?//?讀取參數(shù)并獲取硬盤序列號
HdSerial?=?line.substring(line.indexOf("卷的序列號是?")?+?"卷的序列號是?".length(),?line.length());
break;
}
}
}?catch?(IOException?e)?{
e.printStackTrace();
}
return?HdSerial;//?返回硬盤序列號?卷的序列?非物理
}
當(dāng)前標(biāo)題:硬盤序列號java代碼 硬件編碼和硬盤序列號
鏈接URL:http://jinyejixie.com/article48/dodhjep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供動態(tài)網(wǎng)站、品牌網(wǎng)站設(shè)計(jì)、電子商務(wù)、網(wǎng)站建設(shè)、域名注冊、定制網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)