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
169k42 gold badges224 silver badges441 bronze badges
asked May 9, 2013 at 3:26
7
  • Runtime..exec Have you considered updating to ProcessBuilder? Need to support 1.4 or before? Commented May 9, 2013 at 3:48
  • i am working with java6 Commented May 9, 2013 at 3:51
  • Hi, should i upgrade to ProcessBuilder Commented May 9, 2013 at 3: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. ;) Commented May 9, 2013 at 4:03
  • thnx but for your reply but i am unable to comprehend much. Can you please ellaborate as i am new to Java Commented May 9, 2013 at 4:10

1 Answer 1

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.

answered May 9, 2013 at 3:29
Sign up to request clarification or add additional context in comments.

3 Comments

my file is /mobilityapps/testScript/testScript.sh absolute path whereas i am executing it from /opt/java6/jre/bin
do you get any values for your system outs? They may have useful information for debugging.
also do you have +x bit (aka execute bit) set for the shell script?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.