Forums Register Login

Having trouble in running a unix command and getting the output

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
Willie Smits increased rainfall 25% in three years by planting trees. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1069 times.
Similar Threads
Pesky Runtime method
how to handle different charset with ProcessBuilder?
java program run external command on Unix
executing linux commands through java socket
How to connect to Linux using Java code?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 10:49:56.