【博客搬家】jni学习体会***

jni学习体会【aboykoy原创】
1 获得内存信息
2 获得CPU主频信息

================= ================== ================== ================
首先是1
目录 C:/c/hardware/com/swjtu/hardinfo
根目录 C:/c/hardware
================= ================== ================== ================
hardinfo中:
Memo.java、gethardinfo.java、
Memo.java :
-----------------------------------------------
package com.swjtu.hardinfo;
class Memo
{
/*
public static native void MemoStr();
static
{
System.loadLibrary("Memo");
}

public static native void MemoryStatus();
static
{
System.loadLibrary("Memo");
}
*/
public static native int percent();
static
{
System.loadLibrary("Memo");
}
public static native long total();
static
{
System.loadLibrary("Memo");
}
public static native long free();
static
{
System.loadLibrary("Memo");
}
/*
public static native void paging();
static
{
System.loadLibrary("Memo");
}

public static native void pagingper();
static
{
System.loadLibrary("Memo");
}

public static native void vmemory();
static
{
System.loadLibrary("Memo");
}

public static native void freevmemory();
static
{
System.loadLibrary("Memo");
}
*/
}
------------------------------------------
gethardinfo.java
--------------------------------------------
package com.swjtu.hardinfo;
class gethardinfo
{
public static void main(String[] args)
{
long total=0,free=0;
int percent=0;
percent=Memo.percent();
total=Memo.total();
free=Memo.free();

System.out.println(percent);
System.out.println(total);
System.out.println(free);
}
}
------------------------------------------
命令顺序:
C:/c/hardware/com/swjtu/hardinfo>javac gethardinfo.java
C:/c/hardware/com/swjtu/hardinfo>javac Memo.java
C:/c/hardware/com/swjtu/hardinfo>javah com.swjtu.hardinfo.Memo
C:/c/hardware/com/swjtu/hardinfo>cl -IC:/Java/jdk1.5.0_02/include -IC:/Java/jdk1
.5.0_02/include/win32 -LD com_swjtu_hardinfo_Memo.cpp -FeMemo.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
com_swjtu_hardinfo_Memo.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/dll
/implib:Memo.lib
/out:Memo.dll
com_swjtu_hardinfo_Memo.obj
   Creating library Memo.lib and object Memo.exp
C:/c/hardware/com/swjtu/hardinfo>cd..
C:/c/hardware/com/swjtu>cd..
C:/c/hardware/com>cd..
C:/c/hardware>java com.swjtu.hardinfo.gethardinfo
64
784492
278344
-----------------------------------------------------------
javah com.swjtu.hardinfo.Memo
命令生成文件:com_swjtu_hardinfo_Memo.h  其内容如下:
-----------------------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_swjtu_hardinfo_Memo */
#ifndef
_Included_com_swjtu_hardinfo_Memo
#define _Included_com_swjtu_hardinfo_Memo
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_swjtu_hardinfo_Memo
* Method:    percent
* Signature: ()I
*/
JNIEXPORT jint JNICALL Java_com_swjtu_hardinfo_Memo_percent
  (JNIEnv *, jclass);
/*
* Class:     com_swjtu_hardinfo_Memo
* Method:    total
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Memo_total
  (JNIEnv *, jclass);
/*
* Class:     com_swjtu_hardinfo_Memo
* Method:    free
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Memo_free
  (JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
---------------------------------------------------------------------------
根据这个头文件,自己建立一个com_swjtu_hardinfo_Memo.cpp文件,内容如下:(其中函数命名一
定要和H文件中的一样)
---------------------------------------------------------------------------
#include "com_swjtu_hardinfo_Memo.h"
#include "windows.h"
#include "stdio.h"
#include "WINREG.H"
#define DIV 1024
char *divisor = "K";
#define WIDTH 7
//extern "C"
JNIEXPORT jint JNICALL Java_com_swjtu_hardinfo_Memo_percent
  (JNIEnv *, jclass)
{
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return stat.dwMemoryLoad;
}
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Memo_total
  (JNIEnv *, jclass)
{
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return stat.dwTotalPhys/DIV;
}
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Memo_free
  (JNIEnv *, jclass)
{
MEMORYSTATUS stat;
GlobalMemoryStatus (&stat);
return stat.dwAvailPhys/DIV;
}
-----------------------------------------------------------------------
cl -IC:/Java/jdk1.5.0_02/include -IC:/Java/jdk1
.5.0_02/include/win32 -LD com_swjtu_hardinfo_Memo.cpp -FeMemo.dll
这个命令LINK com_swjtu_hardinfo_Memo.cpp文件生成DLL文件Memo.dll,将这个文件拷贝到工程的
根目录(即:C:/c/hardware)下,运行命令
C:/c/hardware>java com.swjtu.hardinfo.gethardinfo

结果::::::::::
64
784492
278344
================= ================== ================== ================
然后是2
目录 C:/c/hardware/com/swjtu/hardinfo
根目录 C:/c/hardware
================= ================== ================== ================
文件:Zhupin.java、Getzhupin.java
Zhupin.java内容:
--------------------------------------------------------------------
package com.swjtu.hardinfo;
public class Zhupin {
public static native long get();
static
{
System.loadLibrary("zhupin");
}
}
--------------------------------------------------------------------
Getzhupin.java内容:
--------------------------------------------------------------------
package com.swjtu.hardinfo;
class Getzhupin {
public static void main(String[] args)
{
long zhupin=0;
zhupin=Zhupin.get();
System.out.println(zhupin);
}
}
-----------------------------------------------------------------------
首先,敲命令:

C:/c/hardware/com/swjtu/hardinfo>javac Zhupin.java
C:/c/hardware/com/swjtu/hardinfo>javac Getzhupin.java
Getzhupin.java:8: cannot find symbol
symbol  : variable Zhupin
location: class com.swjtu.hardinfo.Getzhupin
zhupin=Zhupin.get();
       ^
1 error
这个问题在命令行模式下没有解决,于是在Eclipse中建立两个CLASS文件,将代码粘贴进去,顺利
生成CLASS文件Zhupin.class/Getzhupin.class,将这两个文件拷到
C:/c/hardware/com/swjtu/hardinfo里去。
运行命令:
C:/c/hardware/com/swjtu/hardinfo>javah com.swjtu.hardinfo.Zhupin
生成了文件com_swjtu_hardinfo_Zhupin.h,其内容如下:
----------------------------------------------------------------------
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_swjtu_hardinfo_Zhupin */
#ifndef
_Included_com_swjtu_hardinfo_Zhupin
#define _Included_com_swjtu_hardinfo_Zhupin
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class:     com_swjtu_hardinfo_Zhupin
* Method:    get
* Signature: ()J
*/
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Zhupin_get
  (JNIEnv *, jclass);
#ifdef __cplusplus
}
#endif
#endif
-------------------------------------------------------------------------
同样,自己建立com_swjtu_hardinfo_Zhupin.cpp文件,内容如下:(注意函数名)
-------------------------------------------------------------------------
#include "com_swjtu_hardinfo_Zhupin.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <WINREG.H>
//#include "WINREG.H"
//#pragma   comment(lib,"mainfre.lib")

//extern "C"
JNIEXPORT jlong JNICALL Java_com_swjtu_hardinfo_Zhupin_get
  (JNIEnv *, jclass)
{
  

LPCTSTR key="HARDWARE//DESCRIPTION//System//CentralProcessor//0";
HKEY hkey;

DWORD dwVal;
DWORD WordType = REG_SZ;
DWORD WordData =100;
long ret=RegOpenKeyEx(HKEY_LOCAL_MACHINE,key,0,KEY_READ,&hkey);
LPTSTR lpValueName="~MHz";
if(RegQueryValueEx(hkey,lpValueName,NULL,&WordType,(LPBYTE)&dwVal,&WordData)==
ERROR_SUCCESS)
{
return dwVal;
//printf ("cpu %ld /n",dwVal);
}
return 0;
}
-------------------------------------------------------------------------
运行命令:
C:/c/hardware/com/swjtu/hardinfo>cl -IC:/Java/jdk1.5.0_02/include -IC:/Java/jdk1
.5.0_02/include/win32 -LD com_swjtu_hardinfo_Zhupin.cpp -Fezhupin.dll
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8168 for 80x86
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
com_swjtu_hardinfo_Zhupin.cpp
Microsoft (R) Incremental Linker Version 6.00.8168
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/dll
/implib:zhupin.lib
/out:zhupin.dll
com_swjtu_hardinfo_Zhupin.obj
   Creating library zhupin.lib and object zhupin.exp
com_swjtu_hardinfo_Zhupin.obj : error LNK2001: unresolved external symbol __imp_
_RegQueryValueExA@24
com_swjtu_hardinfo_Zhupin.obj : error LNK2001: unresolved external symbol __imp_
_RegOpenKeyExA@20
zhupin.dll : fatal error LNK1120: 2 unresolved externals
出现了错误
打开VC,文件-新建-【工程】-MFC AppWizard(dll)-工程名:zhupin 位置:
C:/c/hardware/com/swjtu/hardinfo-确定-确定---FileView--右击Source Files 选添加文件到目
录,把com_swjtu_hardinfo_Zhupin.h和com_swjtu_hardinfo_Zhupin.cpp添加进去,点“建立”,
,,,,,,出现一下错误:
Cannot open include file: 'jni.h': No such file or directory
在百度上搜索,得到如下解决方案:
是在vc的tools/options/directory中加入jni.h和jni_md.h两个文件所在的绝对路径,
在/jdk/include和/jdk/include/win32下
如上修改后,顺利编译生成DLL文件。
将DLL文件拷贝到C:/c/hardware里 敲命令:
C:/c/hardware>java com.swjtu.hardinfo.Getzhupin
1662
记录完毕。
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值