File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Running commands using Runtime.getRuntime().exec() in Unix Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Running commands using Runtime.getRuntime().exec() in Unix" Watch "Running commands using Runtime.getRuntime().exec() in Unix" New topic
Author

Running commands using Runtime.getRuntime().exec() in Unix

Arjun Murthy
Greenhorn

Joined: May 11, 2011
Posts: 10
Hi,

How do i run commands which contain piping(|). I believe that being a shell feature it wont be recognized by java.

What i am trying to do is something like :

the output i get returns all the processes and doesn't seem to go beyond the piping.

Is there any solution to this?
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1132

Arjun,
Method exec is not a UNIX shell interpreter.
If you want to use it for something more complex than a single, simple UNIX command, I suggest creating a shell script and launching the shell script from the exec method.
If you haven't already done so, I suggest you read the following article from JavaWorld:

When Runtime.exec() won't

Good Luck,
Avi.
Arjun Murthy
Greenhorn

Joined: May 11, 2011
Posts: 10
Thansk for your reply Avi.
In the command i want to run "ps -ef | grep /home/abcd" the "home/abcd" part is not known beforehand. I am getting that as an input and creating the command runtime so i dont think making a shell script would be possible. Is there any other way?

Thanks
Avi Abrami
Ranch Hand

Joined: Oct 11, 2000
Posts: 1132

Arjun,
You wrote:

In the command i want to run "ps -ef | grep /home/abcd" the "home/abcd" part is not known beforehand. I am getting that as an input and creating the command runtime so i dont think making a shell script would be possible.

Of-course it would be possible, since shell scripts accept command-line arguments.
Perhaps you need to brush up on your UNIX?
Allow me to suggest the Unix CD Bookshelf.

Good Luck,
Avi.
Arjun Murthy
Greenhorn

Joined: May 11, 2011
Posts: 10
I am a beginner to unix so i apologize for the ignorance. However the input is coming from a form in a web application and not command line. Would that make any difference?
Arjun Murthy
Greenhorn

Joined: May 11, 2011
Posts: 10
Thanks for the help Avi.

Upon further googling I found the following solution



 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Running commands using Runtime.getRuntime().exec() in Unix