Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Arjun Murthy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1141
1
Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the help Avi.

Upon further googling I found the following solution



 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic