1

I want to run a shell script from a java program. My shell script(loop_shell.sh) is working fine when I am running it in terminal and even my Java program(Execute.java) is working fine while running with other commands like 'ls' etc. But when I try run shell script from the Java program, shell script is not printing the values which are inside a loop. Here are the codes for both shell script and Java Program respectively.

loop_shell.sh:-

 #bin/sh

i=0

j=0

timer=0;

echo "u r in loop_shell.sh"

while((i!=1));

do

 echo "waiting for the folder" 

done

Execute.java:-

import java.io.BufferedReader;

   import java.io.IOException;

   import java.io.InputStream;

   import java.io.InputStreamReader;

   import java.io.File;



   public class Execute
   {

    public static void main (String args[])
    {

     String command="./loop_shell.sh";

     String output=executeCommand1(command);

     System.out.println(output);
    }
        public static String executeCommand1(String command) {

            StringBuffer output = new StringBuffer();

            Process p;

            try {
                File dir = new File("/home/vamz/Desktop/sudhir_personal/JAVA_IMPORTANT/");//path
                p = Runtime.getRuntime().exec(command,null,dir);
                p.waitFor();
                BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                            String line = "";           
                while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
                }

            } catch (Exception e) {
        e.printStackTrace();
        }

    return output.toString();

    }

  }

Output:- u r in loop_shell.sh

Expected Output:-

 u r in loop_shell.sh
 u r in loop_shell.sh
 u r in loop_shell.sh
 u r in loop_shell.sh
 u r in loop_shell.sh
 u r in loop_shell.sh
 ..........so on ....

If u try to run both the programs u will get the same output. U can see that output is just printing "u r in loop_shell.sh" and qutting with out waiting for shell script to complete!! Can some one pls explain me what is happening ? and pls tell me how to run infinte loop script from a java program.

2 Answers 2

2

After searching in internet and scratching my head for long time I have found out the correct way to run a infinite looped shell script from a java program. MY java program has to be like this:-

import java.io.BufferedReader;

   import java.io.IOException;

   import java.io.InputStream;

   import java.io.InputStreamReader;

   import java.io.File;



   public class Execute
   {

    public static void main (String args[])
    {

     String command="/bin/bash loop_shell.sh"; ----> change in shell command!

     String output=executeCommand1(command);

     System.out.println(output);
    }
        public static String executeCommand1(String command) {

            StringBuffer output = new StringBuffer();

            Process p;

            try {
                File dir = new File("/home/vamz/Desktop/sudhir_personal/JAVA_IMPORTANT/");//path
                p = Runtime.getRuntime().exec(command,null,dir);
                p.waitFor();
                BufferedReader reader = 
                            new BufferedReader(new InputStreamReader(p.getInputStream()));

                            String line = "";           
                while ((line = reader.readLine())!= null) {
                output.append(line + "\n");
                }

            } catch (Exception e) {
        e.printStackTrace();
        }

    return output.toString();

    }

  }

As u can see the bash command has to be like this "/bin/bash/ loop_shell.sh" .Now my java program is waiting for the shell script to complete!!

Sign up to request clarification or add additional context in comments.

1 Comment

Are there any ways to run infinite looped shell script from java ? If yes pls share!!
0

It is an infinite loop, and you are calling a "p.waitFor()" that waits till the command has finished. That is the problem, just let the script finish and you will see the result.

change the script to:

i=0

j=0

timer=0;

 echo "u r in loop_shell.sh"

while((i!=10));

do

    echo "waiting for the folder" 
    let i=i+1
done

2 Comments

Thanks Celeb for the answer!! You did'nt answer my question "How to run infinite loop shell script from a java program?". U r trying to say that we cannot run infinte loop shell from a Java program?
If the problem is with p.waitFor() my java program has to wait indefinetly.But it is not happening like that. It is just quitting after printing "u r in loop_shell.sh".U can see that if u try running both the programs!

Your Answer

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.