0

I have tried the following codes both have different approaches.

Approach 1: It breaks when executing in the latest version of Eclipse IDE say 2020-03 whereas it works fine in Mars IDE.

This problem is already asked on this How to display asterisk for input in Java?.

Approach 1:


Package test;

import java.io.*;

public class Test {
    public static void main(final String[] args) {
        String password = PasswordField.readPassword("Enter password:");
        System.out.println("Password entered was:" + password);
    }
}

class PasswordField {

    public static String readPassword(String prompt) {
        EraserThread et = new EraserThread(prompt);
        Thread mask = new Thread(et);
        mask.start();

        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String password = "";

        try {
            password = in.readLine();
        } catch (IOException ioe) {
            ioe.printStackTrace();
        }
        et.stopMasking();
        return password;
    }
}

class EraserThread implements Runnable {
    private boolean stop;

    public EraserThread(String prompt) {
        System.out.print(prompt);
    }

    @SuppressWarnings("static-access")
    public void run() {
        while (!stop) {
            System.out.print("\010*");

            try {
                Thread.sleep(1);
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }

        }
    }

    public void stopMasking() {
        this.stop = true;
    }
}

Approach 2:

It does not work on Eclipse IDE but does work on the command line.


import java.io.Console;
public class Main {

    public void passwordExample() {        
        Console console = System.console();
        if (console == null) {
            System.out.println("Couldn't get Console instance");
            System.exit(0);
        }

        console.printf("Testing password%n");
        char[] passwordArray = console.readPassword("Enter your secret password: ");
        console.printf("Password entered was: %s%n", new String(passwordArray));

    }

    public static void main(String[] args) {
        new Main().passwordExample();
    }
}

Community
  • 1
  • 1

1 Answers1

0

Your program for approach 1 doesn't work because it seems the eraser thread isn't closed. You should change your initial boolean value on stopMasking to false, and then tell set it to true after your catch. There's a number of resources available with code that does EXACTLY this. The best one I found is here:

http://www.cse.chalmers.se/edu/year/2018/course/TDA602/Eraserlab/pwdmasking.html

That runs in my Eclipse 2020 build. As far as your second approach, console does not work for user input within an IDE, hence the issues in there but none from command line. If you want to use this version, you could replace console with a scanner:

 import java.util.Scanner

 Scanner input = new Scanner(System.in);
 String password = input.nextLine();
 //implementation

I'd also suggest importing specific class files you need. Importing all of java.io will bog down your program. Hope that helps!

Zach Rieck
  • 419
  • 1
  • 4
  • 23
  • Thanks for the quick reply Zach. I have tried with approach 2 with the link you have provided. Still, i am getting the same problem - Eclipse IDE gets hanged. The version of eclipse i am using - 2020-03. I would advise to try in your system with latest IDE. Looking forward to your advice. – Ranjit Singh Apr 03 '20 at 16:22
  • @Ranjit the link is for method 1, not 2. I see what you're talking about though. The Eclipse IDE does crash when I implement the method from the link provided, although it seems to run fine from the command line. I wonder if this is a potential bug. I'll do some more research and get back to you – Zach Rieck Apr 06 '20 at 16:47
  • Yes, i meant about approach 1. Apologies for that. Sure please let me know if you will find anything. Just to let you know in case if it helps in reseaech--I have tested the same code on Mars version of eclipse. It works perfectly there. – Ranjit Singh Apr 06 '20 at 16:49
  • Just to know- Did you downside my question? If yes, may i know why? – Ranjit Singh Apr 06 '20 at 16:53
  • @Ranjit nope wasn't me. If I had to guess, someone from the community saw this code was available and this question had been asked, but there's no information on a bug for that version of Eclipse, so I think the question is valid – Zach Rieck Apr 07 '20 at 00:47
  • thanks for the response. Can you please up the vote so that more developers can see the post and we will have good discussion on this thread. If you feel so. – Ranjit Singh Apr 07 '20 at 04:17