1

I am trying to execute netstat command from java using runtime execution, but it throwing below IOException.

It works fine for other commands, even the synonym command onetstat is working fine. I am trying understand why netstat alone is failing and how to make it work. any help is appreciated.

java.io.IOException: Cannot run program "netstat": netstat: not found
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:1059)
    at java.lang.Runtime.exec(Runtime.java:629)
    at java.lang.Runtime.exec(Runtime.java:462)
    at java.lang.Runtime.exec(Runtime.java:359)
    at com.ca.RunCmd.executeCommand(RunCmd.java:30)
    at com.ca.RunCmd.main(RunCmd.java:18)
Caused by: java.io.IOException: netstat: not found
    at java.lang.UNIXProcess.fullPath(UNIXProcess.java:306)
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Gangadhar Kairi
  • 470
  • 4
  • 11
  • `netstat: not found`. What other commands work? If onetstat is a synonym, where is onetstat found? – Bill Woodger Oct 12 '15 at 12:44
  • commands like ls -l, cd etc are working. only the netstat command is failing. if you want to know about [onetstat](https://www-01.ibm.com/support/knowledgecenter/SSLTBW_2.1.0/com.ibm.zos.v2r1.halu101/unixsyn.htm) or man netstat in uss – Gangadhar Kairi Oct 12 '15 at 13:04
  • 1
    So, does netstat work for you from a command prompt? Since netstat is a synonym of onetstat, you implied the other way around, perhaps the synonymonisation does not happen, or not correctly, with the way you are trying to access it. The actual command is onetstat, so I'd use that, but useful to find out why the synonym doesn't work for you. – Bill Woodger Oct 12 '15 at 13:19
  • it works fine from the command prompt. onetstat is also giving me the same response. Only thing that concerning me both commands are same but one is failing other is working. I think understanding the why netstat is failing would give more understanding about the onetstat. – Gangadhar Kairi Oct 13 '15 at 08:42
  • `netstat` is a synonym of `onestat`. Use the documentation of netstat for onetstat. It would seem that the synonym is established in the shell, but not when you try to use it through the code. There's some "start up" which is making the synonym for the shell (guessing) which doesn't happen (the same way) when you do it through Java. There must be something in the documentation for your Java if you want to get it going with netstat but since it is a synonym and the actual command is onetstat discovering the difference is good, but only useful for when there is no alternative. Use onetstat. – Bill Woodger Oct 13 '15 at 08:51

2 Answers2

2

To really understand what's going on here, you'll need to hunt down the actual executable, whether netstat or onetstat.

If netstat is aliased to onetstat in the shell - depending on which shell you're running, the "whence" or "alias" command will tell you. A simple solution might be to run the command via the shell (/bin/sh -c netstat) rather than running netstat directly.

Another possibility is that these commands are what's known as an "external link"...a way for a UNIX Service pathname to point to a conventional executable in a z/OS dataset. If this is the case, then you very well might have the netstat/onetstat in your path and otherwise correct, but you might not have the correct STEPLIB or LNKLST concatenation. When the system exec's the target (netstat/onetstat), it doesn't find the externally-linked program, and you get the "not found".

All sorts of things can go wrong here, especially when external links are involved. There can be system (APF) authorization issues, missing modules in your STEPLIB/LNKLST, not enough memory to load the program, etc etc etc. Unfortunately UNIX Services on z/OS doesn't always interpret every possible failure code, so sometimes it's necessary to go hunting. A good first start would be to catch the exception you're getting and look for the ERRNO/ERRNO2 values - they can give you a good hint.

If you have traditional z/OS facilities, your friend is the console log...SDSF's Log function or equivalent. There very well might be an x06 abend and a CSV... message on the console that would give you the clues about what to do next.

Valerie R
  • 1,769
  • 9
  • 29
  • Interesting. Doesn't it make you want to sit down in front of the actual system and find it? :-) – Bill Woodger Oct 15 '15 at 01:19
  • Hi Valerie R, What you said seems to be correct. I tried executing the alias commands they also throwing the same IOException. This makes it clear to me that netstat is alias to the onetstat command. but i am not sure how to find, i didn't find the netstat in default list aliases(alias). Thanks much Valerie and Bill for providing my very insightful information. – Gangadhar Kairi Oct 15 '15 at 15:27
  • To clarify, when I said "hunt down the actual executable", I meant to look at the directory entry for the netstat/onetstat executable. It might just not be in your path, or it might be an issue like I pointed out with external links and your STEPLIB/LNKLST. Just another thought is that sometimes z/OS security policy prevents certain programs from being run, and this can also appear to be a variation on "not found". This is where viewing the console messages might be a big timesaver... – Valerie R Oct 16 '15 at 00:01
1

The solution is to use the actual command instead of the alias command in this case the actual command is onetstat, netstat is an alias command. The problem is with the alias commands, when you try execute a alias command through the java runtime its failing to execute them. I am yet to find the exact reason but the problem could be replicated easily by the creating an alias for any command try executing them, you can try this in windows environment as well.

Gangadhar Kairi
  • 470
  • 4
  • 11
  • OK, thanks. We can remove the obsolete comments now. Hover over your comment and click the x-in-a-circle that appears. Keeps things cleaner, and easier for future searches to follow. – Bill Woodger Feb 22 '16 at 13:32