JAVA runtime exec throwing IOException for netstat command in mainframe USS environment

818 Views Asked by At

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)
2

There are 2 best solutions below

3
Valerie R On BEST ANSWER

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.

1
Gangadhar Kairi On

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.