JNI调用C/C++方法从控制台输入密码

分类: J2SE   出处:iocblog整理  更新时间:2009-09-19   添加到收藏  

最近看到一个问题,如何用java实现从控制台输入密码?
    本来以为是很简单的问题,查了一下发现java居然没提供这样一个方法。目前实现的方式有2个,一个是利用jni来调用c/c++方法,另一个是使用多线程。
下面是使用jni的方法:
首先,写出我们的java类:


public   class  jnipasswordreader  {

    
private   native  string readpassword();
    
static {
        system.loadlibrary(
" passworddll " );
    }

    
/**
     * 
@param  args
     
*/

    
public   static   void  main(string[] args)  {
        
//  todo auto-generated method stub
        jnipasswordreader reader  =   new  jnipasswordreader();
        string pwd 
=  reader.readpassword();
        system.out.println(
" your password is: "   +  pwd);
    }


}

    这一段使用system.loadliberary("..");来加载本地类库,passworddll是文件名,不需要加dll后缀,系统会自动辨认。

编译成jnipasswordreader.class以后,使用
javah -jni jnipasswordreader
命令,生成一个jnipasswordreader.h文件,文件内容如下:
 

/*  do not edit this file - it is machine generated  */
#include 
< jni.h >
/*  header for class jnipasswordreader  */

#ifndef _included_jnipasswordreader
#define  _included_jnipasswordreader
#ifdef __cplusplus
extern   " c "   {
#endif
/*
 * class:     jnipasswordreader
 * method:    readpassword
 * signature: ()ljava/lang/string;
 
*/

jniexport jstring jnicall java_jnipasswordreader_readpassword
  (jnienv 
* , jobject);

#ifdef __cplusplus
}

#endif
#endif

然后,我们需要写一个cpp文件来实现
jniexport jstring jnicall java_jnipasswordreader_readpassword  (jnienv *, jobject);
接口。
于是,我写了一个passworddll.cpp文件,内容如下:

//  这是主 dll 文件。
#include  " stdafx.h "
#include 
" jnipasswordreader.h "
#include 
< iostream >
#include 
< iomanip >
#include 
< conio.h >
using   namespace  std;

/*
 * class:     jnipasswordreader
 * method:    readpassword
 * signature: ()v
 
*/

jniexport jstring jnicall java_jnipasswordreader_readpassword
  (jnienv 
*  env, jobject) {
      
char  str[ 20 =   { 0 }
    jstring jstr;
    
char  ch;
    
char   * pstr  =  str;
    
while ( true )
    
{
        ch 
=  getch();
        
if (isdigit(ch) || isalpha(ch))
        
{
            cout
<< " * " ;
            
* pstr ++   =  ch;
        }

        
else   if (ch  ==   '  '   &&  pstr  >  str)
        
{
            
* ( -- pstr)  =   0 ;
            cout
<< "   " ;
        }

        
else   if (ch  ==   0x0a   ||  ch  ==   0x0d )
        
{
            
break ;
        }

    }

    jstr 
=  env -> newstringutf(str);
    
return  jstr;(文章来源 www.iocblog.net)
}

    我使用vs2005来生成对应的dll文件,在生成之前,需要把$jdk_home/include/jni.h和$jdk_home/include/win32/jni_md.h这两个文件copy到microsoft visio studio 8/vc/include目录下,我就在这里卡了大概1个小时,一直说找不到jni.h文件

   然后就可以使用vs2005来生成dll了,生成好对应的passworddll.dll以后,把该dll文件放到系统变量path能找到的地方,比如windows/system32/或者jdk/bin目录,我是放到jdk_home/bin下面了
放好以后,
执行java jnipasswordreader
就可以输入密码了。

关于jni的更详细内容,可以参考:
《在windows中实现java本地方法》http://www.ibm.com/developerworks/cn/java/jnimthds/index.html

我的password.dll文件
http://www.blogjava.net/files/richardeee/passworddll.zip(文章来源 www.iocblog.net)


Tag: JNI



文章整理:iocblog
版权申明:本站文章均来自网络,如有侵权,请联系我们,我们收到后立即删除,谢谢!
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有。