1

I am learning Java from Java doc and I want to implement the examples, but I am facing some problems. Can anyone describe me this problem, and tell me how can i get values from console using eclipse IDE.

Whenever, I execute this example, the output is No Console.

Code i've built so far:

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;
    }
}
surajs1n
  • 1,493
  • 6
  • 23
  • 34
Atul Rai
  • 332
  • 1
  • 10
  • 25

1 Answers1

2

Eclipse connects to your program using simple input and outputstreams with no console support. You cannot use the Console class for this reason. This is a limitation for all IDE's that are build with Java as Java does NOT provide support for this when making processes.

While you cannot use your program directly from eclipse, you are able to launch it without problems from the commandline. This can be done by exporting you program as a runnable jar file, then using CMD or bash to go to that directoy, then doing the command java -jar <name_of_file>.

Ferrybig
  • 18,194
  • 6
  • 57
  • 79