How to get response from "exec" function?

Hello!
I’m using WM-4.6.
There is “sample.commandLineExec:fireCommandExec” java service in WMSamples package.
This code is from there:

String execFile = “dir c:”;
Process child = Runtime.getRuntime().exec(execFile);
OutputStream outCommand = child.getOutputStream();
String outStream = outCommand.toString();

At finish value of outStream does output into Pipeline-Out.

But when I’m trying to call this external command (or something like “ls -la” for UNIX), then I’m getting outStream=null.

So, I need to get true output stream (or string) with results of the called command. May somebody helps me - how to do it?
Thank you!

Hi
Please the java code for the service that you can use to make system calls.
/**
This service is used for making Operating System Calls
It takes 3 input parameters

para1 =“/bin/sh”;
para2 =“-c”;
para3 = OS command to execute.

OS commands

ls -lt

grep -i

Output :

Output of the OS command in a string .
*/

IDataCursor idc = pipeline.getCursor();
// String cmd = {
// “/bin/sh”,
//“-c”,
// "prtconf |grep -i firmware "
// };

try{
idc.first();
String cmd= new String[3];
for(int i=0;i<3;i++)
{
cmd[i]=(String)idc.getValue();
System.out.println(cmd[i]);
idc.next();
} ;
String s = null;
Process p = Runtime.getRuntime().exec(cmd);
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));

    BufferedReader stdError = new BufferedReader(new 
             InputStreamReader(p.getErrorStream()));
String strOutput =new String();

    // read the output from the command
    //System.out.println("Here is the standard output of the command:\n");
   	while ((s = stdInput.readLine()) != null) {
        
  		//  System.out.println(s);
	strOutput += s+'\n';
       }
        
        // read any errors from the attempted command

        //System.out.println("Here is the standard error of the command (if any):\n");
   	String strError =new String();    
   	while ((s = stdError.readLine()) != null) {
       	// System.out.println(s);
    	  strError += s+'\n';
      };


idc.insertAfter("output",strOutput);
idc.insertAfter("error",strError);
	idc.destroy();

}
catch (Exception e)
{throw new ServiceException(e.toString());
}

HTH
Yogesh

Thanks a lot, Yogesh!

Your code works very fine and fast!

But be careful - there is a trouble with some external commands. Some ones throws IOExceptions. For example (in Windows-2000):

  • trying to call “dir c:” in Windows throws “java.io.IOException: CreateProcess: dir c: error=2” (this record is from server.log file of IS)
  • but “find <from_file>” executes correct.

I have created the same topic on advantage.webmethods.com. Members of that forum have made some useful comments about this functionality (execute of external commands):

====
I believe exec’ed processes on Windows will block if they output more characters than the JVM output buffer size. Be aware, if you are running on Windows and the exec’ed command has a lot of output, you need to run separate threads to read from the output and error streams, or the process will hang. You should at least test this scenario before putting the service into production.

===
“dir c:” will fail the exec on Windows. This is because, dir isn’t an executable, it is a built-in (interpreted) operation of the Windows command shell. Make sure whatever you exec on Windows is a .exe, .com or some other directly executable file.

… but it is possible to call something like this: cmd /c “dir c:”