Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unix commands from java always return null on HP-UX

 
Mahita Ande
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18272
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Richard Tookey
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 1166
17
Java Linux Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic