想用java实现在命令行下输入密码,密码以“*”显示,想了很久,在网上也找了很久就是没结果,请帮忙啊!我开发用的是jdk1.4的,所以jdk6.0上的方法就不要介绍了! 谁帮我解决了分还可以多给。

解决方案 »

  1.   

    随便再问题下,那位有可用在虚拟的solaris下的用于解压缩的lha包吗?
      

  2.   

    看一下这个
    http://211.147.225.32/gate/big5/java.chinaitlab.com/Swing/526545_2.html
      

  3.   


    这里有答案:http://topic.csdn.net/t/20050726/21/4169966.html
      

  4.   

    看一下这个例子:4   Java命令行程序密码屏蔽输入实现方式     
        
        与基于AWT或者JSwing的图形用户界面程序相比,在基于命令行的Java程序中实现密码屏蔽输入要比较麻烦一些,原因在于JDK没有提供任何基于文本的字符回显控制方法,因此需要编写相应的控制代码。基于通用性方面的考虑,在本文中将编写用于屏蔽用户输入的对象InputMasking,该对象的定义如下:     
        
      //InputMasking.java   
      import   java.io.*;   
        
      public   class   InputMasking   
      {   
                      String   getPassword(String   initial)   throws   IOException     
                      {   
                              MaskingThread   listeningthread   =   new   MaskingThread(initial);   
                              Thread   thread_instance   =   new   Thread(listeningthread);   
                              String   password   =   "";   
                              thread_instance.start();                   
                              while   (true)     
                              {   
                                        char   input   =   (char)System.in.read();   
                                        listeningthread.stopMasking();   
                                        if   (input   ==   '\r')     
                                        {   
                                                    input   =   (char)System.in.read();   
                                                    if   (input   ==   '\n')     
                                                            break;                                   
                                                    else                                   
                                                            continue;                                   
                                        }   
                                        else   if(input   ==   '\n')                             
                                                            break;                           
                                              else                             
                                                            password   +=   input;                           
                                }   
                                return   password;   
                    }   
      }   
          
        
        
        该对象在后台启动线程的控制下,从系统输入设备中读取字符并对该字符进行分析。如果遇到行结束标志,则返回该线程获取的字符串对象password。读者一定会关心后台线程对象MaskingThread的作用,该线程对象周期地刷新终端窗口,其目的在于屏蔽用户输入的字符,使该字符不能够在窗口中显示出来。该线程对象的定义为:     
        
      //MaskingThread.java   
      import   java.io.*;   
        
      class   MaskingThread   extends   Thread     
      {   
            private   boolean   stop   =   false;   
            private   int   index;   
            private   String   initial;   
        
            public   MaskingThread(String   initial)     
            {   
                  this.initial   =   initial;   
            }   
            public   void   run()     
            {   
                  while(!stop)     
                  {   
                        try     
                        {                           
                              this.sleep(1);   
                        }   
                        catch   (InterruptedException   ex)     
                        {   
                              ex.printStackTrace();   
                        }   
                        if   (!stop)     
                        {   
                              System.out.print("\r"   +   initial   +   "   \r"   +   initial);   
                        }   
                        System.out.flush();   
                  }   
            }   
            public   void   stopMasking()     
            {   
                  this.stop   =   true;   
            }   
      }   
          
        
        
        在InputMasking对象和MaskingThread对象的配合下,使得基于命令行的Java应用程序能够实现用户输入密码字符的屏蔽,其核心方法是利用后台线程时时刷新终端窗口,屏蔽用户输入字符。下面的CmdLineUtility对象即利用前面定义的两个对象进行用户输入屏蔽,请读者实际运行上述程序,以了解命令行Java程序屏蔽输入的方式:     
        
      //CmdLineUtility.java   
      import   java.io.*;   
        
      public   class   CmdLineUtility   
      {   
            public   static   void   main(String   argv[])     
            {   
                  InputMasking   masking   =   new   InputMasking();   
      String   password   =   null;               
                  try     
                  {   
                        password   =   masking.getPassword("请输入登录密码:   ");   
                  }     
                  catch(IOException   ex)     
                  {   
                        ex.printStackTrace();   
                  }   
                  System.out.println("您输入的密码为:   "   +   password);   
            }   
      }   
      

  5.   

    Java的标准输入流里没有回显字符,如果需要,不嫌麻烦,可以看看System.in实现的源代码,重写一下不知可不可以,;)
      

  6.   

    c代码
    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    #include <stdio.h >       
    #include <conio.h >  
    /* Header for class test_InputMasking */#ifndef _Included_test_InputMasking
    #define _Included_test_InputMasking
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     test_InputMasking
     * Method:    getPassword
     * Signature: (I)Ljava/lang/String;
     */
    JNIEXPORT jstring JNICALL Java_test_InputMasking_getPassword
    (JNIEnv * env, jobject o, jint maxlength){
                       char  pas[1024]={0},i;                     for(i=0;i<maxlength;i++)       
                        {       
                                       
    pas[i] = getch(); 
    if(pas[i]==13){break;} 
                            putchar( '*');   

                        }
    return (env)->NewStringUTF(pas);
       
    }#ifdef __cplusplus
    }
    #endif
    #endif
    java代码package test;/**
     * 
     * @author   test
     * @version  1.0.0
     * @2007-10-18 9:22:33
     */
    public class InputMasking {
        {
            System.loadLibrary("test");
        }
        public native String getPassword(int maxlength);
    }调用时直接
    String password=new InputMasking().getPassword(10);
    就可以了
      

  7.   

    不过这个好像只能在命令行下运行,我在eclipse下执行时有点问题。
      

  8.   

    谢谢各位啊!试试piaopiao11 的方法。
      

  9.   

    http://211.147.225.32/gate/big5/java.chinaitlab.com/Swing/526545_2.html就用下面这个/* DO NOT EDIT THIS FILE - it is machine generated */ 
    #include  <jni.h > 
    #include  <stdio.h  >        
    #include  <conio.h  >   
    /* Header for class test_InputMasking */ #ifndef _Included_test_InputMasking 
    #define _Included_test_InputMasking 
    #ifdef __cplusplus 
    extern "C" { 
    #endif 
    /* 
     * Class:     test_InputMasking 
     * Method:    getPassword 
     * Signature: (I)Ljava/lang/String; 
     */ 
    JNIEXPORT jstring JNICALL Java_test_InputMasking_getPassword 
    (JNIEnv * env, jobject o, jint maxlength){ 
                       char  pas[1024]={0},i;                      for(i=0;i <maxlength;i++)        
                        {        
                                        
    pas[i] = getch();  
    if(pas[i]==13){break;}  
                            putchar(  '* ');                        } 
    return (env)- >NewStringUTF(pas); 
        
    } #ifdef __cplusplus 

    #endif 
    #endif 
    java代码 package test; /** 
     *  
     * @author   test 
     * @version  1.0.0 
     * @2007-10-18 9:22:33 
     */ 
    public class InputMasking { 
        { 
            System.loadLibrary("test"); 
        } 
        public native String getPassword(int maxlength); 
    } 调用时直接 
    String password=new InputMasking().getPassword(10); 
    就可以了
      

  10.   

    package net.xiaohai;import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JPasswordField;/**
     * @author haihai
     * 
     */
    public class TestMain extends JFrame{
    JPanel p=null;
    JPasswordField pf=null;
        public TestMain() {
         super("密码屏蔽");
         p=new JPanel();
         pf=new JPasswordField(15);          //密码输入框〈〈〈〈
         pf.setEchoChar('●');    //掩码〈〈〈〈〈
         p.add(pf);
         this.getContentPane().add(p);
         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         this.setSize(300, 300);
         this.setVisible(true);
    }
        
        public static void main (String[] args) {
         new TestMain();
        }
        
    }
    是不楼主要的
      

  11.   

    还是JNI好用啊,不过,楼主为什么不用JDK1.6呢?工程需要吗?
      

  12.   

    AntiRSI 的可以那个我用过原创一个老外写的,另外给你个想法我记得一年前我写一个聊天程序发现 telnet时候输入的内容是看不见的,使用java通讯利用这个可以使用telnet和另一台机器聊天(这台机器监听),不过时间比较久远了我记不住了,呵呵。这周比较累,要不就帮你翻翻以前的代码了。
      

  13.   

    如果sunyijia先生能给我找找你以前的实现方式,真是感激不尽啊!这个问题困扰我太长时间了。
    工程需要,只能在JDK1.4下开发。
    AntiRSI 的方法我已经试过了,确实不行,F1,F3,F7等键没有屏蔽掉,上次输入的密码会被记录。