Skip to Main Content

Java Programming

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!

How to run Unix command with Pipe ( | )?

807588Jun 10 2009 — edited Jun 10 2009
Dear Friends,
I have to execute the below unix command through java program.

*ps -ewwo pid,args | grep -E "abc.sh|xyz.jar" | gawk '{ print $1 }' | wc -l*


My code to execute this command is,
Runtime run = Runtime.getRuntime();
		
File workDir = new File("/root/sat");
String psCmd = "ps -ewwo pid,args | grep -E \"abc.sh|xyz.jar \" | gawk '{ print $1 }' | wc -l";

Process process = run.exec(psCmd, null, workDir);
		
		String line;
		
		int i = process.waitFor() ;
		if(i == 0) {
		BufferedReader buf = new BufferedReader( new InputStreamReader( process.getInputStream() ) ) ;

		while ( ( line = buf.readLine() ) != null )
		{
			System.out.println("Line - ["+line+"]") ;
		}
		} else {
			BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
			// read the output from the command
			while ((line = stdErr.readLine()) != null) {
			System.out.println(line);
			}

			}
When i execute this command, i'm getting output as,

ERROR: Garbage option.

When i analyse the above error, i found that, the PIPE ( | ) command is not supported through java.


Could anyone please help me how to execute the above unix command (with | ) in java?



Thanks in advance,
Sathish
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 8 2009
Added on Jun 10 2009
5 comments
376 views