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?

Community
  • 1
  • 1
user3847620
  • 59
  • 4
  • 9

2 Answers2

2

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

Look at this questions for example code:

Community
  • 1
  • 1
BetaRide
  • 16,207
  • 29
  • 99
  • 177
  • 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? – user3847620 Jul 17 '14 at 07:20
  • No. You have to write to the (wrapped) `InputStream` you get from the process. `System.out.println` always prints to the console. – BetaRide Jul 17 '14 at 07:23
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

Jp Vinjamoori
  • 1,173
  • 5
  • 16