barbara
barbara

Reputation: 3201

How to redirect a stream to simple Java application?

I have a file data.txt. Its content is:

a b c
d e f

I also have Java code

public class Main {
    public static void main(String[] args) {
        System.out.println("size: " + args.length);
        for (String arg : args) {
            System.out.println("arg: " + arg);
        }
    }
}

And a script run:

#!/bin/bash
java -jar test.jar "$@"

I want to redirect a file as stream to my app.

I tried the following:

cat data.txt | ./run

Which gave me:

size: 0

But I want to see:

size: 6
a
b
c
d
e
f

How can I accomplish this?

Upvotes: 0

Views: 119

Answers (1)

Andy Turner
Andy Turner

Reputation: 140318

You can invoke with xargs to concatenate the contents of stdin with the command line:

cat data.txt | xargs ./run

Depending upon the value of $IFS, this may be equivalent to:

./run a b c d e f

Upvotes: 1

Related Questions