Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Executing 'grep' and 'who' with pipe in Java

843836Aug 29 2003 — edited Sep 3 2003
Hi!

I am trying to execute following command using a Java program.
grep username | who | wc -l

I have written following code.

String command = "/bin/bash -c grep username | who | wc -l";
Process p = Runtime.getRuntime().exec(command);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p1.getInputStream()));

BufferedReader stdError = new BufferedReader(new
InputStreamReader(p1.getErrorStream()));

// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s= stdInput.readLine()) != null)
{

System.out.println(s);
}

// read any errors from the attempted command
System.out.println("Here is the standard error of the command (if any):\n");
while ((s= stdError.readLine()) != null)
{
System.out.println(s);
}
//System.exit(0);
I am getting following message at my prompt:
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

Also if I want to run
$who > temp
in java, can anyone please tell me how to run it?

thanks
bandya
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Oct 1 2003
Added on Aug 29 2003
8 comments
617 views