I am adding a command line interface to a Java program. It receives arguments on the command line and some input from the keyboard. It displays a text message to prompt for each of the necessary keyboard inputs.
If someone redirects the System.in from a file, the display text should be suppressed.
My first approach is to run the following check at startup:
System.in.available() > 0
Is this reliable or is there a better way of distinction?
Related
i have two threads: one of them (server) is printing something to the console and the other (user) one is taking input from console. The problem occurs if the first thread prints something while user is typing into console. it looks like for example
command from usermessage from server
or if the user hasn't typed the command fully
commmessage from server
and it messes up the command from the user
is there a way to make it look like
message from server
command from user
or
message from server
comm
so to print one line above the currently typed text in the console and maintain the user input. i still want the first thread to print while the user is typing so locking this thread untill user enters the command isn't an option
[I want] to print one line above the currently typed text in the console and maintain the user input.
There are no easy answers here unfortunately. The user input being entered is actually being echoed by the terminal and not by the Java program. You have no control over the input and can't reprint it once the standard output is displayed.
The only way to do this otherwise is to turn off terminal echoing and control the user keystrokes in your program, but that makes it a lot more complicated. See: How to disable console echoing
You could also use some sort of Java dialog box and then pop the standard out message in another dialog or let it go to standard out.
i still want the first thread to print while the user is typing so locking this thread until user enters the command isn't an option
Yeah I fear that locking out the standard output messages is only way for you to do this unless you want to use a different mechanism to get your user input.
I am making a Java program that get arrow keys and characters as input without pressing enter, I have searched the web, and the only "solution" was putting the TTY in raw mode. I do not want to do that, since that does not work on windows.
EDIT: I am read from the console. Scanner and System.in does not return data until Enter is pressed.
I'm sure some people are familiar with the concept. You run a command at the CLI and as it progresses, the one line in front of you updates with a percentage. Under the bonnet, before each line is printed, it clears the current line, so instead of 100 lines of progress updates, you just have one line which updates until it hits 100%.
I want to make an app that will echo this information out into an embeded diaplay window. I'm fairly new to Java and I'd like my app to run a Windows command (sfc /scannow to be exact), display the output to the user on-the-fly and then once complete, I want to analyse the log file and give a readable conclusion to the user.
Is there some kind of library or special way of going about doing this so that when the line is "updated" in the console window, I can simply overwrite the current line in my display window?
The line is probably updated by the program writing \r control character (carriage return) to stdout. It is the task of the receiving application to handle this. The normal terminal handles this by setting the cursor to the start of the line. Your program can intercept this character and also treat it by resetting the line.
I pass command line arguments to my java code in eclipse (and retrieve it using args[] in main method)-this is fine.
However, my scenario is different. My code periodically asks for input during execution. Where would I enter such input? when controls hits such input prompt, eclipse freezes
EDIT
Some of the answers I read below suggests using command line args or buffered reader or using console view: but my eclipse freezes right after asking me for sudo password (further, my situation is different from command line args as explained in the very first line).
I am using Kepler on centos 6.5
Eclipse has a console, its typically located at the bottom of your java perspective. If its not there just go to Window->Show View->Console.
Have you ever used an input stream reader or a buffered reader?
The following is a start on how
static InputStreamReader input = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(input);
Then you can use the buffered reader for taking input during execution.
you can pass command line arguments from eclipse, run your program from eclipse as run on configuration it will open dialog box and click arguments tab and pass your arguments from there
Go to Window -> Show View -> Console. You will able to enter input.
For short cut , type : Alt + Shift + Q, C.
Use the Scanner class. You have to import it from java.util.Scanner;
String userInput;
Scanner input = new Scanner(System.in);
System.out.print("Enter something: ");
userInput = input.nextLine();
and remember to close the scanner when you're done using it
input.close();
Puzzled why my question is down voted and why few respondents suggested irrelevant answer.
You can pass input using scanner or input stream if your code is expecting it. But my question is asking for uncertain situations(in this case, if my eclipse is started as sudo it wont ask for root password; but if I start as regular user it would ask for root password based on the Linux shell command that my java code has to exec)
Yes, you can pass the input to java code (executed under eclipse). From the command line window where eclipse was started, we can enter the input and java code can read it as string.
I want to add a "history" function to my java programm, like known from bash etc, so pressing the arrow keys should show previous send commands.
It's no problem to write the past commands to the default output, which will in three new lines if arrow up is hit three times and in not editable output. I want the output of the programm to be written in the input field so i just have to hit enter, to resend the command.
Is this possible?
Kind Regards
Take a look at JLine, which provides command history, tab completion and line editing.
If you want to roll your own solution, this will get you started.
You want to change from using a buffered input into a direct input. You can do this by interfacing with System.in directly. You should create a thread to handle this, and have it block on a call to System.in.read() in a loop, reading one byte at a time.
Each time a byte is read, keep your own buffer updated with the current command that's being read. Every character that gets typed, add it to the buffer. If the character is a \b, delete the last character in the buffer. When you detect a \r or \n, execute the command in the buffer and clear it.
If you receive an up or down arrow, send a number of \bs to System.out equal to the length of the buffer. This will erase the local copy of any current command being entered. Then print out the new command to System.out and enter it into the buffer, replacing whatever was there. This will allow the user to delete it, add to it, or just press enter to submit it. This mimics the functionality of bash.
You can also detect a \t (tab) character and implement a tab-completion function.