2

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?

2 Answers 2

2

Grab the InputStream from the Process and write the y into it.

Look at this questions for example code:

Sign up to request clarification or add additional context in comments.

2 Comments

Hi thanks for the answer. I am new to Java so I am a bit confused with the terms used. Based on my understanding of your answer, it means to use the InputStream and the BufferedReader to read the line and finally to use System.out.println to write 'y' into that line?
No. You have to write to the (wrapped) InputStream you get from the process. System.out.println always prints to the console.
1

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

2 Comments

Do I include that "myApp.exe < yes.txt" under the batch file or after the Runtime and Process?
You have to write a batch file with "myApp.exe < yes.txt" and call the batch file from within java program

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.