Not able to Run Shell Script using Java Service

Hi All,

I am trying to run a Shell Script using a java Service. It always gives an IO Exception and not running the Script.

String cmds = new String[2];
cmds[0] = “ls -la”
cmds[1] = “pwd”

// execute the command
Process pro = Runtime.getRuntime().exec(cmds);
// wait until it’s done executing

// what did the process output from the Input pipe back to
// this process (okay, who named this stuff)?
InputStream out = pro.getInputStream();

// output it (really slowly)
int i;

while ((i = != -1) System.out.println((char) i);


This is the code which is trying to run the Script. The shell script basically remove the files from the directory based on the create date.

/usr/bin/find /work/b2b/valunet/temp -mtime +14 -exec rm {} ;

Any Suggestions.

Muru, try replacing your “while” code with the following:

try { 
      while (true) { 
        // Get the bytes and return as an int 
        int i =; 
        // -1 indicates the end of the stream 
        if (i == -1) break; 
        // Cast the int into a char 
        char c = (char) i;  
catch (IOException e) { 

Also, don’t forget that Unicode characters are generally not handled properly within input and output streams; The casting of the byte into a char will return a ISO Latin-1 character.

Thanks Dan,

No luck. I tried Using the one in Toolbox i get the same result IO Exception. I am attaching that code here. To handle this as a short term solution we delete all the files in the directory using a java service and schedule it for once in a week.

This is the code from ToolBox.

//Input Cursor
IDataHashCursor pipelineCursor = pipeline.getHashCursor();

//Declare Input and Output Variables
String executionParams = null;
String stdout = " ", exitValue = " ", stderr = " ";
Object executionException = new Object();

if ( pipelineCursor.first ( “executionParams” ) )
executionParams = (String)pipelineCursor.getValue();

//Try to execute the shell command

Process p;
//Execute the command
p = Runtime.getRuntime().exec(executionParams);

//Get exitValue
int exitvalue = p.exitValue();
exitValue = Integer.toString(exitvalue);

//Get the stdout and error streams of the process object
//Note: The input stream obtains data piped from the standard output stream
//of the process it represents.
InputStream inputStream = p.getInputStream();
InputStream errorStream = p.getErrorStream();

//Get stderr
BufferedReader errbfr = new BufferedReader( new InputStreamReader ( errorStream ) );
StringBuffer errstrbfr = new StringBuffer();
int b;

while ( ( b = (int) != -1 )
char c = (char) b;

stderr = errstrbfr.toString();

//Get stdout
BufferedReader outbfr = new BufferedReader ( new InputStreamReader (inputStream) );
StringBuffer outstrbfr = new StringBuffer();
int d;

while ( (d = (int) != -1)
char e = (char) d;

stdout = outstrbfr.toString();
catch (Exception e)
IDataHashCursor pipelineCursor_1 = pipeline.getHashCursor();
pipelineCursor_1.insertAfter( “executionException”, e);

IDataHashCursor pipelineCursor_1 = pipeline.getHashCursor();
pipelineCursor_1.insertAfter ( “stdout”, stdout );
pipelineCursor_1.insertAfter ( “stderr”, stderr );
pipelineCursor_1.insertAfter ( “exitValue”, exitValue );

Can you litter your code with “System.out.println” to identify where the code fails? Maybe that will help spot the issue.

In the meanwhile, it is good to know that you have a workaround.

This might be a permissions issue. To get my “shell via java” program to work, I needed to modify a file called java.policy.


I am still looking in to that Service to get the proper error message.


Which java file we need change we are running b2b server on solaris box i can see more than one java.policy files i can see one in b2b server/jvm/lib/. What needs to be changed if you can give me more details that would help us lot.


Hi Muru - try splitting the “-al” from the “ls” as a seperate cmds array entry.

We’ve had the same problems. We found using a command format of “sh -c command” to be the most reliable solution. i.e. In terms of your program, this worked for us

cmds[0] = “/bin/sh”
cmds[1] = “-c”
cmds[1] = “find blah blah blah”