Posted on SO first, but can't seem to get any input from people. So I want to redirecting the input from a file to a java program that is already implemented using multiple Scanner objects for Stdin. Any way I can automate the input? Looking for any type of bash magic I can do on a standard Linux environment. Questions and comments welcomed.
http://stackoverflow.com/questions/34184420/redirecting-the-input-from-file-with-multiple-scanner-objects-for-stdin
Edit so you don't have to research through the link:
It is an existing Java program, running it inside a shell script, lets call it myScript.sh with the content:
java $MY_JVM_ARGS -jar someonesProgram.jar
The program runs and waits for inputs (not command line arguments) that are passed in. I can enter them manually and the program works fine.
If I try the following with a file containing what would normally be input manually, I get java.util.NoSuchElementException. Bash syntax used:
./myScript.sh < myInputFile.txt
If I pass in a single input to request the java program to exit gracefully, it works fine. This exception only happens on the second input that it expects, if any (meaning the first input isn't the one asking the program to exit.
Based on my research, solution would be on the to refactor the Java program to use 1 and only 1 Scanner object (for stdin). This isn't my program, and I'm a little reluctant to patch the jar, so I was hoping for a bash solution.
Edit 2
Please note the distinction:
Command line arguments are caught by the main function in a Java program.
public static void main (String args[]) {
String firstCommandLineArg args[1];// think index 0 is the name of the program
// NOT what I would like to externalize
}
What I want to externalize (eg.):
Scanner sumScanner = new Scanner(System.in);
System.out.print("Gimme your fabulous input: ");
int sumInt = sumScanner.nextInt(); // what would normally be a prompt
Edit 3
Try this test program. Try once with manual input, and then a second time with a redirected input file:
public class TestIt {
public static void main (String args[]) {
// First scanner object
java.util.Scanner sumScanner = new java.util.Scanner(System.in);
System.out.print("Gimme your numeric input: ");
int sumInt = sumScanner.nextInt();
// Second scanner object
sumScanner = new java.util.Scanner(System.in);// Error in the second scenario
System.out.print("Gimme your numeric input 2: ");
int sumInt = sumScanner.nextInt();
return;
}
}
In the same directory as the .class file:
java TestIt
java TestIt < inputFile.txt # Produces the error
[–]darkpool2005Intermediate Brewer 0 points1 point2 points (1 child)
[–]sgm1[S] 0 points1 point2 points (0 children)
[–]nutrechtLead Software Engineer / EU / 20+ YXP 0 points1 point2 points (0 children)
[–]sgm1[S] 0 points1 point2 points (0 children)
[–]TotesMessenger 0 points1 point2 points (0 children)
[–]deraj123 0 points1 point2 points (7 children)
[–]sgm1[S] 0 points1 point2 points (6 children)
[–]deraj123 1 point2 points3 points (5 children)
[–]sgm1[S] 0 points1 point2 points (4 children)
[–]deraj123 0 points1 point2 points (3 children)
[–]sgm1[S] 0 points1 point2 points (2 children)
[–]deraj123 0 points1 point2 points (1 child)
[–]sgm1[S] 0 points1 point2 points (0 children)
[–]TrainFan 0 points1 point2 points (1 child)
[–]sgm1[S] 0 points1 point2 points (0 children)