How to read user data from BeanShell JConsole?

I am trying to create a simple console and I found this: Create a command "Console

I decided to give it a try and it seems perfect for my need. The only problem is that I cannot get user input from the user because the BufferedReader is locked. I am not familiar with BufferedReader and JConsole, so I cannot figure out what needs to be fixed.

Here is my version of the code:

import java.io.*;
import bsh.util.*;
import java.awt.*;

public class CLI extends JConsole
{       
    public CLI()
    {
        Font font = new Font("Consolas", Font.BOLD, 12);
        setFont(font);

        new InputThread().start();
    }

    private class InputThread extends Thread
    {
        BufferedReader input = new BufferedReader(getIn());
        String newline = System.getProperty("line.separator");
        String line = "";
        String prompt = "$ ";

        public void run()
        {
            try
            {
                do
                {
                    print(prompt, Color.RED);
                    line = input.readLine();
                    print("You typed: " + line + newline, Color.BLUE);

                } while (!line.equalsIgnoreCase("quit"));

                input.close();
            }
            catch (IOException e)
            {
                e.printStackTrace();
            }
        }
    }
}
+3
source share

All Articles