0

From a Powershell script I would like to run another application, which in this case is a Java app, and when that app asks a question via stardard in prompt I would like PowerShell to give it a response.

The Java app don't accept this data via command line arguments , instead it must be run interactively and expects responses to it's prompts. When run interactively the it goes through the following questions:

Question: Select your application? Answer: 7

Q: Are you sure? A: Y

Q: Online[1] or offline[2] A: 1

I've been able to get this to work automatically using 'echo' only for the first question via this script:

echo 7 | java -jar javaapp.jar

But attempts to get it to send to the subsequent prompts are ignored: commas between the options, `n [new lines] between the letters, or spaces, are all ignored.

Can anyone suggest an alternative way of doing this? Thanks

  • Try this way: `7, 'Y', 1 | java -jar javaapp.jar` (without `echo`!) – zett42 Jan 14 '22 at 11:34
  • Thanks @zett42: This looked so simple and promising but this also doesn't work. Infact it gives the same message as when I was using `echo` at the start of the line. When it reaches the second prompt: immidately after the prompt the error text "ERROR: No line found" appears (and the process ends and it drops back to the prompt). Could it be that PowerShell is sending the input too quickly? Doing this from Windows CMD would normally have timeouts between the inputs. – eddieddieddie Jan 16 '22 at 07:50
  • After thinking about this for a while a long and wondering if maybe I could use the PowerShell equivalent of how it might be done in Windows CMD I came up with `7; start-sleep 2; 'Y'; start-sleep 2; 1 | java -jar javaapp.jar`. This doesn't work either - only the last element is passed to the java process, then trying various combinations of ( or { around the optioning commands doesn't work either. I guess what I needed here is a way to start the Java process but keep the PowerShell script running in parallel so it can push responses to the questions. – eddieddieddie Jan 16 '22 at 08:08
  • You don't need a "sleep", because feeding a process from standard input is synchronized. PowerShell waits until the process actually requests input, before sending the next line. Maybe there is an issue with line endings (Unix vs. Windows style). – zett42 Jan 16 '22 at 14:07
  • I just verified that it is basically working by writing a small Java app similar to [this one](https://stackoverflow.com/a/4645193/7571258) (the 2nd example that uses BufferedReader). Do you have source code of the Java app? – zett42 Jan 16 '22 at 15:06
  • Another try: `(7, 'Y', 1) -join "\`n" | java -jar javaapp.jar` – zett42 Jan 16 '22 at 15:09
  • Sadly that doesn't work also - at the second prompt it errors with the same message as before: "ERROR: No line found". Unfortunately I don't have the source code of the Java app. – eddieddieddie Jan 16 '22 at 22:05
  • You could try a [Java decompiler](http://www.javadecompilers.com/). – zett42 Jan 16 '22 at 23:24
  • I was hoping to avoid that! – eddieddieddie Jan 17 '22 at 03:19
  • Ok, just another try. Create a file "input.txt" with 7, Y, 1 on separate lines. Use cmd.exe to feed input to the Java app: `cmd /c java -jar javaapp.jar – zett42 Jan 17 '22 at 08:29
  • Thanks for contining to come up with ideas on this one! I tried your suggestion above and all line ending type (or at least those availabe in Notepad++) fail on the secont question with the same _"ERROR: No line found"_ message. I tried Windows CR LF, Unix LF, and even Macintosh CR - the same on all three. – eddieddieddie Jan 17 '22 at 22:08
  • Incidentally I have found a way to get this to run automatically but using VB script or via use 'System.Windows.Forms' in PowerShell. The following runs and works... `Start-Process java -argumentlist "-jar javaapp.jar", "-x" Add-Type -AssemblyName System.Windows.Forms [System.Windows.Forms.SendKeys]::SendWait('7{ENTER}') Start-Sleep 5 ...` However this is not the solution I'm looking for as it can't be run from an Azure deployment pipeline - it fails with permission denied, I suspect because it's not running interactively. – eddieddieddie Jan 17 '22 at 22:16

0 Answers0