0

Hi i am trying to run shell script from following code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

 public class ScriptTest {
     public static void main(String[] args){


    BufferedReader stdErr=null;
    BufferedReader stdIn=null;
    try{   
    System.out.println("In Script");
    String[] commands= {"ls"};
    Process process  = Runtime.getRuntime().exec("/mobilityapps/testScript/testScript.sh");
    stdIn=  new BufferedReader(new InputStreamReader(process.getInputStream()));        
    stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream()));
    String inline= stdIn.readLine();
    String errline =stdErr.readLine();
    System.out.println("*Inline*"+inline);
    System.out.println("*Errline*"+errline);
    while(inline!=null){
        System.out.println(inline);
        inline=stdIn.readLine();
    }
    while(errline!=null){
        System.out.println(errline);
        errline=stdErr.readLine();
    }
    System.out.println("Process Exit Value: "+process.waitFor());
    }catch(Exception excp){
        excp.printStackTrace();
    }
}

}

The script i am trying to call is

CURRDATE=`date '+%d%b%Y'`
TIMESTAMP=`date '+%H:%M'`
BASE_PATH=/mobilityapps/testScript
LOGFILE=${BASE_PATH}/logs_${CURRDATE}_${TIMESTAMP}.log
echo ${CURRDATE} ${TIMESTAMP}>>${LOGFILE}

All both the script and Java program are in the same directory. When i run testScript.sh from PUTTY it runs fine

But when i run from Java program Process Exit Value is 255

Can anyone suggest the changes?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Sohail Khan
  • 279
  • 1
  • 3
  • 10
  • `Runtime..exec` Have you considered updating to `ProcessBuilder`? Need to support 1.4 or before? – Andrew Thompson May 09 '13 at 03:48
  • i am working with java6 – Sohail Khan May 09 '13 at 03:51
  • Hi, should i upgrade to ProcessBuilder – Sohail Khan May 09 '13 at 03:55
  • 1
    The thing is. Most use of `Runtime.exec` is by rank amateurs who fail to consume the streams, and hand it multiple arguments in a single `String`. `ProcessBuilder` encourages multiple arguments & makes it easier to consume the streams (by offering to merge them). OTOH In your example both output streams are consumed, and there is just 1 argument. ..It is borderline whether it is worth it! Though I just have a vague feeling you are better off using the latter API simply because Oracle would care if they got a bug report about it. ;) – Andrew Thompson May 09 '13 at 04:03
  • thnx but for your reply but i am unable to comprehend much. Can you please ellaborate as i am new to Java – Sohail Khan May 09 '13 at 04:10
  • OK - stick with `Runtime.exec`. Let's not complicate things! :) – Andrew Thompson May 09 '13 at 04:11
  • but i am still not able to run the script successfully – Sohail Khan May 09 '13 at 04:24

1 Answers1

2

Try replacing the path

    Runtime.getRuntime().exec("/mobilityapps/testScript/testScript.sh");

with

    Runtime.getRuntime().exec("./mobilityapps/testScript/testScript.sh");

If you just use / at the begining, it means that it's a absolute path. Using '.' indicates that is a relative path.

OscarSan
  • 464
  • 8
  • 24