Auto-input 'y' when prompted in command-line from Java - java

The program I'm using runs through the command prompt from java using a batch file (following the solutions from Run .exe file from Java from file location, and at one point of time, it will prompt the user for an input which is either a 'y' or 'n' in order to continue the program.
How do I program using java codes such that the value 'y' or 'n' is automatically entered when prompted, without requiring users to enter it manually? I have tried using pipe (following the solutions from How do you enter something at a DOS prompt Programmatically?) but it doesn't work. Any ideas?

Grab the InputStream from the Process and write the y into it.
Look at this questions for example code:
Java Process with Input/Output Stream
Reading streams from java Runtime.exec

You can do it the old DOS way.
Create a file, say yes.txt. All it should have is y (as per your app expectations). The file can contain responses to multiple prompts each on their own lines
Now you can execute your exe file something like this
myApp.exe < yes.txt
When exe prompts, yes.txt will supply the prompt text

Related

Send keyboard info to a batch file

I am trying to get a program to automatically send text at a certain time to another program. I created a short Java code that simulates the program I am trying to use for the actual program. The real-life scenario is I am running a .jar file with a batch file and I want a program that will send keyboard inputs to this cmd window and command the .jar from cmd automatically based on the system time. Any help? I am familiar with AutoIT, Java, and basic batch functions. Thanks!
play with this:
While 1
If #HOUR == 22 And #MIN == 31 And #SEC == 15 Then
ShellExecute(#WorkingDir & "\example.jar")
;Sleep(2000)
Send("examplecommand {ENTER}")
EndIf
WEnd

Can someone without JDK run a program that uses the console for i/o (and has no other user interface or display)?

Essentially the question boils down to 'Is there a way to run a class/jar file using the console for input/output without having JDK'. I've written a program in Java for a colleague to use in his work, but he doesn't have/use JDK. I'm aware there are online compilers, but at this point my question has become focused on running a program directly for interest reasons.
Essentially the program is designed to output instructions to the user, the
user makes input in accordance with the instructions, and this goes back and forth for awhile before the program does some calculations based off the input and returns a final result.
I should mention that he has JRE.
My program doesn't use a GUI, it just prints instructions to the console and gathers input from responses typed in the console. It works fine on my computer when running it from the command line ('java myprogram'), but without jdk the java command isn't available to him, which seems to mean he can't run a class file.
My next attempt was to turn the program into an executable jar file, but using the command 'myprogram.jar' from the command line doesn't really do anything. The jar file does include a manifest. I added a blank screen to the code and running the jar file did create the screen, but still no i/o on the console. From what I've read, I think this is because jar files aren't automatically associated with a console, so there is nowhere for the program's output to go or input to come from. He can't use the 'java -jar myprogram.jar' command because he hasn't got the JDK, so even though that command runs the program the way I want it to, it's not an option.
Basically, I'm wondering if it's possible for someone without JDK to run a program and interact with it entirely using the console/command line and no other interface?

Running a .jar file displaying output/parsing commands

I am wanting to run a .jar file and display the output (I'm assuming I would use a rich text box) however I also want to have a text box that will allow me to send commands to the running jar file as if it were running in a command prompt window.
Either this or embedding the command prompt would be ideal. I have messed around with code and have never got it to work.
I would also like to detect certain messages that the jar file returns, for example if a certain command is ran it will display a message depending on whether or not that failed, is there a way to read everything that get returned and then have an event happen if it detects this certain string?
How To: Execute command line in C#, get STD OUT results I have been looking at things such as that, when I say I have messed around with code, I have tried the code given in these examples (changing small parts) they never worked.
To summarise:
- I want to run a jar file that obviously displays it's output in a command prompt.
- I want to hide the command prompt and display it's output in a rich text box.
- The jar file will return string such as 'user 1 has logged in' I want to update a labels text everytime that happens.
You should have a look at the ProcessBuilder and Process classes. They allow to run command line programs and read/write in the process streams.
Also, if your jar is known in advance, why no simply using the class as if it would be in a library and use the exposed APIs?
When we started learning java at school we used
String myInput = JOptionPane.showInputDialog(this, "myPrompt");
and
JOptionPane.showMessageDialog(null, "A basic JOptionPane message dialog");
for input and output. Very simple.
Or you can use the command-line:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in)
String input = br.readLine();

Detecting input redirection file

How can we detect/fetch the name of a file that's fed as an input redirection to a java program from the command line?
Without OS-specific hacks (such as running ps -ef) you can't.
Why do you need this information, and why can't you restructure your program to simply take the file as a command-line argument?

Pass multiple lines of stdin input to interactive Java command line program, non-interactively

I have a command-line java program that takes a password and it's verification from stdin. Unfortunately, modifying the program isn't an option as it is proprietary.
I'm unable to pass the arguments from the unix command line using < since there are two prompts in the program, both asking for the same password. There are stdout statements asking for "Password" and "Password (validation):" on the command prompt.
How can I pass the password non-interactively to this program so that it can be executed automatically using a cron job / RC script?
Thanks!
Chances are that the password is not read from stdin at all, but from /dev/tty. In that case you're out of luck.
Can you run the program by feeding all the input including the passwords through stdin? If not, then you're out of luck.
If this works, however, you can do something like
(echo user; echo passwd1; echo passwd1; cat) | java -jar yourCommandlineprogram.jar
Not sure by your question if Expect is acceptable or not.

Categories

Resources