I have a written a small java program that takes arguments from the commandline and should then be interactive.
When I want to test with large input I want to pipe the contents of a file to the JVM to use as the input arguments to the program but I don't want to replace stdin with the file contents.
So I tried
less file | xargs -0 java -cp ... mypackage.ClassName
but this replaces stdin with the file contents I think so the interactive portion of the program that uses BufferedReader.readLine() takes its content from the file instead.
Is there any way I can first pipe the contents of the file to the JVM then switch back to StdIn?
Im running Ubunthu
Code example:
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String cmd;
while(true) {
try {
System.out.print(">");
cmd = br.readLine(); //becomes null if I start the program as above
Ps. It would be better for the java program to work with files but I want to know if this is possible Ds.
java -cp … --other-argument <contents-of-a-file> --another-argument mypackage.ClassName
? So you can parse the command line for that argument? Maybe you should give us a minimal code example from your Java program? What do you really need to achieve – i.e. what is in this file and why can't you just give your program the file path and let that open the file?java myclass "$(<some-file.txt)"
— but this is a really bad hack and you should modify your Java program to take a file name as an argument.