Knowledge Walls
John Peter
Pune, Maharashtra, India
How to get Password in Core Java with Example
In this example used to get password using readPassword() of interface. System.console() is returning console interface to get the password by readPassword of Console interface.
Example for Getting password on Command Prompt

class Solution {
    public static void main(String args[])throws Exception{
        System.out.println("Enter the username");
        String username = new DataInputStream(;
        System.out.println("Enter the password");
        Console console = System.console();
        char[] password = console.readPassword();
        System.out.println("Username: "+username);
        System.out.println("Password: "+String.valueOf(password));
How it works 

Running on eclipse, System.console() will return NullPointerException.

Previous Topics
Previous lessons of current book.
Computer software engineer articles of One day One Thing to Know
Best Lessons of "One day One Thing to Know"
Top lessons which are viewed more times.
  Copyright © 2014 Knowledge walls, All rights reserved
keep your tutorials and learnings with KnowledgeWalls. Don't lose your learnings hereafter. Save and revise it whenever required.
Click here for more details