使用JNI方法
本地方法调用,需要用到C
你到网上搜一哈 JNI方法使用就明白了
本地方法调用,需要用到C
你到网上搜一哈 JNI方法使用就明白了
解决方案 »
- s2sh管理信息系统的权限管理模块有什么成熟的开发思路?
- 跪求:JAVA分布式消息通信开发方面的资料,麻烦各位仁兄帮帮忙!
- [菜鸟问题:]一个新手对存储过程和视图的疑惑
- 试验Axis webservice 一开始的sayhello都不行
- 在Eclipse3.1+Lomboz3.1+JBOSS4.0开发EJB出现问题:Module type is not compatible
- Struts中如何得到URL参数
- 最近在读hibernate in action,遇到下列术语不懂,请指教
- 请问java如何向一个asp程序发送get请求呢?
- jdom怎么安装配制啊?请高手指点一下,谢谢!
- 何时需要implements java.io.Serializable?等问题请教
- 关于struts中logic:equal标签的一些疑问?
- jsf做的iframe菜单不正常跳转的问题?
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class HardWareID {
String IPCONFIG_COMMAND_WIN = "ipconfig /all";
boolean realMac = true;
String unique = "";
public static String getMacAddress() {
HardWareID hwid = new HardWareID();
return hwid.getUnique().trim();
}
private String getUnique() {
String os = System.getProperty("os.name");
if (os.startsWith("Windows")) {
return getUniqueWindows();
}else {
return "";
}
}
private String getUniqueWindows() {
// 运行控制台命令,返回结果字符串
String ipConfigResponse = null;
try {
ipConfigResponse = runConsoleCommand(IPCONFIG_COMMAND_WIN);
}
catch (IOException e) {
e.printStackTrace();
}
// 按行分割结果字符串,并循环判断每个字符串直道找出 Mac 地址
StringTokenizer tokenizer = new StringTokenizer(ipConfigResponse, "\n");
while (tokenizer.hasMoreTokens()) {
String line = tokenizer.nextToken().trim();
// 获取每行 ":" 后的字符串
int macAddressPosition = line.indexOf(":");
if (macAddressPosition <= 0) {
continue;
}
String macAddressCandidate = line.substring(macAddressPosition + 1).
trim();
// 检查 macAddressCandidate 中内容是否为真实网卡 Mac 地址,根据 Mac 地址计算出唯一标识。
if (isMacAddWin(macAddressCandidate)) {
if (realMac == true) {
generateUnique(macAddressCandidate);
}
else {
realMac = true;
}
}
}
return unique;
}
/**
* 运行控制台命令,返回结果字符串
* @param command String
* @return String
* @throws IOException
*/
private String runConsoleCommand(String command) throws IOException {
Process p = Runtime.getRuntime().exec(command);
InputStream stdoutStream = new BufferedInputStream(p.getInputStream());
StringBuffer buffer = new StringBuffer();
while (true) {
int c = stdoutStream.read();
if (c == -1) {
break;
}
buffer.append( (char) c);
}
String outputText = buffer.toString();
stdoutStream.close();
return outputText;
}
/**
* 对输入参数进行检查,符合正则表达式的为 windows 平台下有效 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddWin(String macAddressCandidate) {
Pattern macPattern = Pattern.compile("[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}-[0-9a-fA-F]{2}");
Matcher m = macPattern.matcher(macAddressCandidate);
return m.matches();
}
/**
* 对输入参数进行检查,符合长度的为 MAC OS X 下有效网卡 Mac 地址
* @param macAddressCandidate String
* @return boolean
*/
private boolean isMacAddOSX(String macAddressCandidate) {
if (macAddressCandidate.length() != 17) {
return false;
}
else {
return true;
}
}
/**
* 产生 Unique
* @param macAddress String
*/
private void generateUnique(String macAddress) {
if (unique == "") {
unique += macAddress;
}
else {
unique += "#";
unique += macAddress;
}
}
public static void main(String[] args) {
System.out.println(HardWareID.getMacAddress());
}
}
这个方法是windows平台下通过运行控制台命令取网卡id。
其实最好的方法还是使用c直接取硬件id,然后使用jni调用c的方法。
另外,不是所有的硬盘,CPU都有序列号。如果要作为机器的唯一标识,建议组合使用。