Reputation: 549
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.
Upvotes: 1
Views: 1507
Reputation: 549
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!!
Upvotes: 2
Reputation: 88
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
Upvotes: 0