简体   繁体   中英

Redirecting the input from file with multiple Scanner objects for Stdin

I am current using a small command line interface for one of our tools.

I would like to to provided input from a file or bash script file to automate the input.

Problem is the CLI uses multiple Scanner object to read input from stdin. There is not a problem inherently when manually inserting input since only one Scanner object is used at any given time, but when stdin in redirected, seems the buffer is attached to the first Scanner object which is only used for the first value read and I receive the java.util.NoSuchElementException .

Just to add, my conclusion on the issue is based on this: Error while redirecting the input from file

Is there any bash magic I can use to redirect stdin in this case? Or maybe even JVM args to assist?

View Reddit Answer

The main idea to this solution would be to have delayed input passed into the script automatically. When the second Scanner object is created, then the expected input should be pushed into standard in, thus getting around the issue of the entire input being caught in only the first scanner.

So instead of having a text file with simple input values, would could have shell script that sleep between each input value.

#!/bin/sh
echo 3
sleep 1
echo 4

And then running the final program as follows:

./delayedEchoScript | java -jar myProgram.jar

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM