I have a JAR file that will require use of the standard input. I need to use this command and cannot change it:
cat filename | java -jar jarname.jar
I want this to be accessible to the JAR somehow, easiest way being through String[] args
. The problem is that this the result of cat filename
doesn't show up in String[] args
, or in any way for me to make use of it. I've tried searching this up for some time, but all I get is questions pertaining to writing java output to files using the pipe.
easiest way being through
String[] args
impossible.
The file will be available as System.in
.
Here's a trivial program that prints sysin right back out, adding line numbers visually. cat
with extra steps. Now you know how to use System.in. Note, if System.in contains byte-based data, obviously don't wrap it in readers (which are for char based data). Just read bytes straight from it.
> cat Test.java
import java.io.*;
class Test {
public static void main(String[] args) throws Exception {
var in = new BufferedReader(new InputStreamReader(System.in));
int lineNum = 1;
while (true) {
String line = in.readLine();
if (line == null) break;
System.out.printf("%6d. %s\n", lineNum++, line);
}
}
}
> cat hello.txt
Hello!
World!
> cat hello.txt | java Test.java
1. Hello!
2. World!