Saturday, May 18, 2013

Reading input and password from command line in Java using java.io.Console - Example tutorial

Java6 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 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 way to read input from command prompt or Console but also reading passwords from 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


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)

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

That's all on how to read input from command line in Java using Console class. As I said there are multiple ways to read input from console and I recommend using Scanner for this task.Though there is nothing wrong in BufferedReader approach as well but 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

2 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

Java67 Headline Animator