aspose file tools*
The moose likes Linux / UNIX and the fly likes unix commands from java always return null on HP-UX  Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Linux / UNIX
Bookmark "unix commands from java always return null on HP-UX  " Watch "unix commands from java always return null on HP-UX  " New topic
Author

unix commands from java always return null on HP-UX

Mahita Ande
Greenhorn

Joined: Sep 26, 2012
Posts: 16
Hi,
I have a java program to get the information from an HP-UX machine. Code is given below:


Process proc = Runtime.getRuntime().exec(new String[] {"/bin/ksh", "-c", "swapinfo | grep memory | awk '{print $2 \" \" $5 \" \" $4}'"});
InputStream op = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(op));
String line = "";
while((line = reader.readLine()) != null);
{
System.out.println(line);
}

this code returns null. I have also tried using '/bin/csh' but the result is still null. As bash is not installed on HP-UX using /bin/bash gave a directory not found error. I also tried executing a simpler command like 'swapinfo' without grep or awk and I also tried the basic 'date' command. The only result I get back is null. I have also tried using ProcessBuilder and that did not make any difference either.

When i run any of the command dirrectly from unix prompt I get the expected result.

may I please be advised on what the problem is and how this can resolved. It would be great if I can have the swapinfo return correct results when using grep and awk options. this will save lot of string parsing.

Thanks,
Mahita
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

When ksh finds a problem with a command it sends the error message to 'stderr' which you completely ignore. You need to process 'stderr' in a separate thread. This and the process return code obtained from Process.waitFor() will give you information about what is wrong.

You would do well to read ALL the sections of http://www.javaworld.com/jw-12-2000/jw-1229-traps.html and then implement ALL the recommendations.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16065
    
  21

Also, it appears that your java code is attempting to read the INPUT stream for the command, not the output (stdout) stream. That simply doesn't make sense.


Customer surveys are for companies who didn't pay proper attention to begin with.
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Tim Holloway wrote:Also, it appears that your java code is attempting to read the INPUT stream for the command, not the output (stdout) stream. That simply doesn't make sense.

Sorry Tim but both 'stdout' and 'stderr' are InputStreams - one reads from them the output written by the process. 'stdin' is the OutputStream and one writes to it to send input to the process!
Mahita Ande
Greenhorn

Joined: Sep 26, 2012
Posts: 16
Thank you for the suggestions Richard. Unfortunately nothing really worked. Whern I use waitFor() for the process I get 0 as return value which I understand indicates a success. Also, I tried to read the stderr but it returns null which implies no error messages. I have also gone through the blog you mentioned. Looks like I am already providing the suggested syntax while using runtime exec(). Still lost

Mahita
Richard Tookey
Ranch Hand

Joined: Aug 27, 2012
Posts: 1057
    
  10

Mahita Ande wrote:Thank you for the suggestions Richard. Unfortunately nothing really worked. Whern I use waitFor() for the process I get 0 as return value which I understand indicates a success. Also, I tried to read the stderr but it returns null which implies no error messages. I have also gone through the blog you mentioned. Looks like I am already providing the suggested syntax while using runtime exec(). Still lost

Mahita


I have a great problem believing that both stdout and stder have no output and you get an awk exit code of zero unless the grep command fails to find 'memory'. Have you run the command from the command line and if so what do you get?

Since you now think your code conforms to the 'traps' article why not post it to make sure?
Mahita Ande
Greenhorn

Joined: Sep 26, 2012
Posts: 16
Minor mistake. I did not use a string[] object to pass in the command arguements. As soon as I used a string array object everything seemed to work.

Thank you Richard for your help.

-Mahita
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unix commands from java always return null on HP-UX