GeeCON Prague 2014*
The moose likes I/O and Streams and the fly likes Having trouble in running a unix command and getting the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Having trouble in running a unix command and getting the output " Watch "Having trouble in running a unix command and getting the output " New topic
Author

Having trouble in running a unix command and getting the output

Suraj Shekar
Greenhorn

Joined: Jul 25, 2002
Posts: 11
Hi,
I am trying to run a unix command from within the java code. I am not able to make it work. I am enclosing the code and the error message that I am getting. Any help is highly appreciated.




The error message that I am getting is

Here is the standard output of the command:

SLu|%%$$=
Here is the standard error of the command (if any):

cat: cannot open |
cat: cannot open A_UNIX_PROGRAM
cat: cannot open -d



Looks like the cat command is working and not the pipe command and the command after the pipe. But when I run the UNIX command from the command prompt I get the expected result.

I am running this program on AIX UNIX machine.
Joe Ess
Bartender

Joined: Oct 29, 2001
Posts: 8914
    
    8

When you run the command from the shell, the shell interpreter understands that you want to run one program and pipe the output to another. When you use Runtime.exec, it takes the entire command line and throws it at cat. Cat tries to open files like "|" and "-p" and fails. You have to exec the shell and pass it the command as arguments or through standard input. Here is a good article on using Runtime.exec and the pitfalls it contains.


"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having trouble in running a unix command and getting the output