How to Read User Input and Password in Java from command line? Console Example

Hello guys, if you are wondering how to take user input from command prompt in Java like username and password then don't worry. Java provides several utilities like Scanner and BufferedReader to read input from command prompt in Java. Java 6 added a new utility class for reading input data from character based devices including command line. java.io.Console can be used to read input from command line, but unfortunately, it doesn't work on most of the IDE like Eclipse and Netbeans. As per Javadoc call to System.Console() will return attached console to JVM if it has been started interactive command prompt or it will return null if JVM has been started using a background process or scheduler job.

Anyway, java.io.Console not only provides a way to read input from command prompt or Console but also reading passwords from the console without echoing it. 

Console.readPassword() method reads password and returns a character array and password is masked during entering so that any peeping tom can not see your password while you are entering it.

Here is a code example of How to read password and input from command prompt or console using java.io.Console. By the way apart from Console, you can also use Scanner or BufferedReader to read input from command prompt, as shown in this example.




Java Program to read password from command prompt using Console class

Here is the Java program which will demonstrate how you can use Console class to read input and password from command line. It's much easier than Scanner or BufferedReader which was alternative solution.

How to read password from command prompt in Java

import java.io.Console;
import java.io.IOException;

public class ConsoleExampleJava {

    public static void main(String args[]) throws IOException  {
  
        Console console = System.console();
        System.out.println("Reading input from console using Console in Java6 ");
        System.out.println("Please enter your input: ");
        String input = console.readLine();
        System.out.println("User Input from console: " + input);
        System.out.println("Reading password from Console in Java: ");
      
        //password will not be echoed to console and stored in char array
        char[] password = console.readPassword();
        System.out.println("Password entered by user: " + new String(password));
    }
}

Output:
Reading input from console using Console in Java6
Exception in thread "main" java.lang.NullPointerException
Please enter your input:
        at ConsoleExampleJava.main(ConsoleExampleJava.java:21)

It's always good to check whether System.console() returns console or null before using it. I haven't used it here just to demonstrate this NullPointerException. Anyway I am still looking to make it work on NetBeans, IntelliJIDEA or Eclipse and will update you guys once I found a way. let me know if you guys know any way to do it.

Here is how you can run it on console:

How to Read User Input and Password in Java from command line? Console Example


You can see that when we entered password its not visible in the screen because we used Console's password feature. If you have used Scanner then it would have been visible because Scanner doesn't differentiate between password or any other text. 

You may also be wondering what is the first error we got, that's actually because the filename and name of public class on Java code was different. Once I corrected that error was gone. 

That's all on how to read input from the command line in Java using the Console class. As I said there are multiple ways to read input from the console and I recommend using Scanner for this task. Though there is nothing wrong with the BufferedReader approach as well Scanner is simply more convenient and more powerful.


Other Java Programming Tutorials from Java67,  You may find useful
Difference between abstract class and interface in Java
10 HashMap examples in Java for programmers
Difference between TreeSet and TreeMap in Java
How to sort using Bubble Sort Algorithm in Java
Difference between Runnable and Thread in Java

5 comments:

  1. If you want to take password as input, make sure you use JPassword from Swing library, so that you can mask characters, due to security reasons.

    ReplyDelete
  2. Doesn't console.readPassword(); provide a non-visible input from console, and so not requiring masking input?

    ReplyDelete
  3. System.out.print("Enter a password: ");
    char [] pw = System.console().readPassword();
    String password = new String(pw);
    System.ou.println("\nYour password input = "+password);

    ReplyDelete
  4. This also generates null pointer exception

    ReplyDelete

Feel free to comment, ask questions if you have any doubt.