Reputation: 31850
I want to send text input to the following Java program from the Unix (bash) command line, so that it will print the text that was entered. How can I write a shell script that will send the string "Print this" to the Java program?
import java.util.Scanner;
public class ReadStuff{
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter some text:");
System.out.println(scan.nextLine());
}
}
Upvotes: 1
Views: 2149
Reputation: 143946
Use echo
echo "Print this" | java ReadStuff
Note that this will output:
Enter some text:
Print
Because you are calling Scanner.next()
which reads the next word, not the entire line.
Or alternatively, if you have stuff in a file:
cat file_with_Print_this | java ReadStuff
Upvotes: 6