重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
利用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;
}
}
}
试试是否可以:)
我是通过一个外部的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);而调用有参数的方法时,则需要将参数在后面依次传入,注意按顺序噢:
一、上Google搜一下Mac及CPU ID的C代码
二、上java.sun.com找那个JNI的指南
三、你用二的方法把一的代码给包一下。
其实利用在java中的runtime来执行在win里面的ipconfig -all的命令可以实现的!!
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就是那!,有关cpu的可以类同实现!
1、在远程server里搭建一个http服务,并支持某种服务端语言(如.net或php之类)
2、用服务端语言获取到服务器的磁盘信息,把结果输出
3、在本地用JS写一段AJAX脚本,远程请求服务端写好的程序,得到结果,并展示给用户
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这种客户端脚本来获得客户端的信息呢?请看以下实例:
!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方式获得客户端的信息主要的优点是,不需求服务器资源.不需求刷新网页.直接在客户端执行脚本获得显示.
在这上找到了硬盘序列号的参数:
\'获得硬盘序列号
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:");// 获取命令行参数
BufferedReader buffreader = new BufferedReader(new InputStreamReader(proces.getInputStream()));
while ((line = buffreader.readLine()) != null) {
if (line.indexOf("卷的序列号是 ") != -1) { // 读取参数并获取硬盘序列号
HdSerial = line.substring(line.indexOf("卷的序列号是 ") + "卷的序列号是 ".length(), line.length());
break;
}
}
} catch (IOException e) {
e.printStackTrace();
}
return HdSerial;// 返回硬盘序列号 卷的序列 非物理
}