java 获取linux序列号_java获取linux下的硬件序列号(特征码)

这篇博客介绍了如何使用Java在Linux环境下获取硬件序列号。通过编写包含本地方法的Java类,调用C语言编写的库文件,实现了从IDE接口读取硬盘序列号并转化为字符串的过程。
摘要由CSDN通过智能技术生成

JAVA类:

package org.hexun.wangchong.admin.gethardware;

public class GetHardWareInfo {

static {

System.loadLibrary("disknum");

}

public native static String getHardWareInfo();

public static void main(String s[]) {

System.out.println(getHardWareInfo());

}

}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在linux终端下生成.h的头文件

javac GetHardWareInfo .java //进到gethardwar目录下

javah org.hexun.wangchong.admin.gethardware.GetHardWareInfo //进行org的父目录下

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

生成的头文件内容如下:

org_hexun_wangchong_admin_gethardware_GetHardWareInfo.h :

/* DO NOT EDIT THIS FILE - it is machine generated */

#include

/* Header for class org_hexun_wangchong_admin_gethardware_GetHardWareInfo */

#ifndef _Included_org_hexun_wangchong_admin_gethardware_GetHardWareInfo

#define _Included_org_hexun_wangchong_admin_gethardware_GetHardWareInfo

#ifdef __cplusplus

extern "C" {

#endif

/*

* Class: org_hexun_wangchong_admin_gethardware_GetHardWareInfo

* Method: getHardWareInfo

* Signature: ()Ljava/lang/String;

*/

JNIEXPORT jstring JNICALL Java_org_hexun_wangchong_admin_gethardware_GetHardWareInfo_getHardWareInfo

(JNIEnv *, jobject);

#ifdef __cplusplus

}

#endif

#endif

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

写一个.c格式的文件,命名为 disknum.c 内容如下:

#include

#include

#include

#include

#include

#include "org_hexun_wangchong_admin_gethardware_GetHardWareInfo.h"

typedef unsigned short WORD;

static char * ConvertToString (WORD diskdata [256], int firstIndex, int lastIndex)

{

static char string [1024];

int index = 0;

int position = 0;

// each integer has two characters stored in it backwards

for (index = firstIndex; index <= lastIndex; index++)

{

// get low byte for 1st character

string [position] = (char) (diskdata [index] / 256);

position++;

// get high byte for 2nd character

string [position] = (char) (diskdata [index] % 256);

position++;

}

// end the string

string [position] = '\0';

// cut off the trailing blanks

for (index = position - 1; index > 0 && ' ' == string [index]; index--)

string [index] = '\0';

return string;

}

static int WaitIde()

{

int al;

while ((al=inb(0x1F7))>=0x80) ;

return al;

}

static void ReadIDE(WORD pw[256])

{

int al;

int i;

for (i = 0; i < 256; i++)

pw[0] = '\0';

ioperm(0x1f0, 16, 1);

WaitIde();

outb(0xA0,0x1F6);

al = WaitIde();

if ((al&0x50)!=0x50) return;

outb(0xA0,0x1F6);

outb(0xEC,0x1F7);

al = WaitIde();

if ((al&0x58)!=0x58) return;

for (i=0;i<256;i++) {

pw[i] = inw(0x1F0);

}

}

JNIEXPORT jstring JNICALL Java_org_hexun_wangchong_admin_gethardware_GetHardWareInfo_getHardWareInfo

(JNIEnv *env, jobject t)

{

char *hd_sn_ptr;

if (sizeof(WORD) != 2)

fprintf(stderr, "%s", "type is error!\n");

WORD pw[256];

ReadIDE(pw);

hd_sn_ptr = ConvertToString (pw, 10, 19);

// fprintf(stdout, "[%s]\n", hd_sn_ptr);

//

jstring str ;

str = (*env)->NewStringUTF(env,hd_sn_ptr);

///

return str;

}

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

在终端下敲入

gcc -I/usr/lib/j2sdk1.5-ibm/include -fPIC -c disknum.c

gcc -shared -WI -soname disknum.o -o libdisknum.so //此处须在disknum前处加一个lib 即libdisknum.so

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

将 libdisknum.so库拷贝到 jdk目录/jre/lib/i386下

终端运行 java GetHardWareInfo 击可

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值