2016-02-15 85 views
1

我正在從Java doc學習Java,我想實現這些示例,但我遇到了一些問題。任何人都可以描述我這個問題,並告訴我如何從控制檯使用eclipse IDE獲取值。無法在java中使用eclipse從控制檯讀取值

每當我執行這個例子時,輸出都是No Console。

代碼到目前爲止,我已經建立:

public class Password { 
    public static void main(String[] args) throws IOException { 
     Console c = System.console(); 

     if (c == null) { 
      System.err.println("No console"); 
      System.exit(1); 
     } 

     String login = c.readLine("Entyer your login password"); 
     char[] oldPassword = c.readPassword("Enter your old password"); 

     if (verify(login, oldPassword)) { 
      boolean noMatch; 
      do { 
       char[] newPassword1 = c.readPassword("Enter your new password"); 
       char[] newPassword2 = c.readPassword("Enter new password again"); 

       noMatch = !Arrays.equals(newPassword1, newPassword2); 

       if (noMatch) { 
        c.format("Password not mathc. Try again.%n"); 
       } else { 
        change(login, newPassword1); 
        c.format("Password for %s change.%n", login); 
       } 

       Arrays.fill(newPassword1, ' '); 
       Arrays.fill(newPassword2, ' '); 

      } while (noMatch); 
     } 
     Arrays.fill(oldPassword, ' '); 
    } 

    static void change(String login, char[] password) { 
     // TODO Auto-generated method stub 
    } 

    private static boolean verify(String login, char[] password) { 
     // TODO Auto-generated method stub 
     return true; 
    } 
} 
+0

System.console()IS NULL – Karthigeyan

+0

檢查此問題http://stackoverflow.com/questions/104254/java-io-console-support-in-eclipse-ide – Karthigeyan

+0

好的,接下來我該做什麼? –

回答

1

使用Eclipse中簡單的輸入連接到你的程序並沒有控制檯支持outputstreams。由於這個原因你不能使用Console類。對於使用Java構建的所有IDE,這是一個限制,因爲在製作流程時,Java不提供對此的支持。

雖然你不能直接從eclipse使用你的程序,但是你可以在命令行沒有問題的情況下啓動它。這可以通過將程序導出爲可運行jar文件,然後使用CMD或bash轉到該directoy,然後執行命令java -jar <name_of_file>來完成。