我用了以下代码去修改用户的口令,但只能修改为空。测试时,发现使用命令passwd accountname后,系统没有返回New password或Re-enter new password字符,因此没有办法传入实际的口令.
代码如下:
 public boolean chgPassword ( String accountname , String password )
    {
        String line = null ;
        boolean result = false ;
        Process Process = null ;
        try {
            Process = Runtime.getRuntime ().exec ( "passwd " + accountname ) ;
            BufferedReader in = new BufferedReader ( new InputStreamReader ( Process.getInputStream () ) ) ;
            OutputStreamWriter out = new OutputStreamWriter ( Process.getOutputStream () ) ;
            while ( ( line = in.readLine () ) != null ) {
                if ( line.indexOf ( "New password" ) != -1 ) {
                    out.write ( "passwd " + password ) ;
                    out.flush () ;
                } else if ( line.indexOf ( "Re-enter new password" ) != -1 ) {
                    out.write ( "passwd" + password ) ;
                    out.flush () ;
                } else if ( line.indexOf ( "Passwd successfully changed" ) != -1 ) {
                    result = true ;
                    out.close () ;
                    in.close () ;
                    break ;
                }
                out = null ;
                in = null ;
                Process.destroy () ;
                Process = null ;
            }
        }
        catch ( Exception ex ) {
            System.out.println ( "create user occur Exception:" + ex.getMessage () ) ;
        }
        return result ;
    }

解决方案 »

  1.   

    对于UNIX用JAVA操作,十有八九就是错,不说Runtime.getRuntime ().exec 是否能和在直接输命令等价(我认为是不等价的,加多了参数,就根本不认,原因不知)==>LZ还是将命令写到SHELL里面
    拿JAVA直接调用SHELL
      

  2.   

    JDK 6里面不是直接有Console对象了么
      

  3.   

    噢?是吗~~不过现在公司有敢直接上JDK6的吗?大多处于观望吧
      

  4.   

    用shell实现不了,因为它是交互式的。