I have a java app that runs a bash file and inside the bash file i have a code to run another java app and it doesn't seem to work.I come from c++ where this is a very easy task but i'm not really experienced in java. So, here's my code:
import java.io.IOException;
import java.net.*;
import java.util.Scanner;
public class start {
public void executeScript(){
try{
ProcessBuilder pb = new ProcessBuilder("/root/Desktop/chat/script.sh");
Process p = pb.start();
p.waitFor();
System.out.println("Script executed..");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String[] args) {
start st = new start();
System.out.println("I'm main..");
st.executeScript();
}
}
Here's my bash file:
#!/bin/bash
echo "bash started"
java Client ip_address
echo "bash finished"
Here's the result of this code:
I'm main.. Script executed..
I know "Script executed.." shouldn't print because the java file i'm trying to run from the bash file is an infinite loop.
Note: If i run the bash file separately in the terminal i get an infinite loop which is the intended result and this is why i know that my mistake is from this file.
I hope I made myself clear and if not please ask for more information. Thank you.
3 Answers 3
Another way of doing would be to use Runtime.getRuntime(). Something like this
public void executeScript() throws IOException, InterruptedException {
Process p = Runtime.getRuntime().exec("sh /root/Desktop/chat/script.sh");
p.waitFor();
BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(p.getErrorStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
line = "";
while ((line = errorReader.readLine()) != null) {
System.out.println(line);
}
}
3 Comments
Client!I'm main.. bash started bash finished Error: Could not find or load main class Client This is weird because as i said before if i run the client separately it works normally!With the above test you can not guaranty that whether it is running or not. Because clearly you told that your are running a infinite loop inside your second java application. Now my advise would be to put some System.out.println statement inside that infinite loop and use below java code to execute your shell script. Here in the output.txt file you can see the output from your shell script as well as java program and you will know whether application executed successfully or not. Also put some echo statement inside your shell script as well.
String[] command ={"/root/Desktop/chat/script.sh", "command line param if any"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.redirectOutput(new File("/tmp/output.txt"));
String result;
String overall="";
try {
Process p = pb.start();
p.waitFor();
BufferedReader br = new BufferedReader(
new InputStreamReader(p.getInputStream()));
while ((result = br.readLine()) != null){
overall = overall + "\n" + result;
}
p.destroy();
System.out.println(result);
} catch (Exception e) {
e.printStackTrace();
}
2 Comments
getErrorStream() and i got this output in the file: I'm main.. bash started bash finished Error: Could not find or load main class Clientif you start a process like you wrote Process p = pb.start();
it will make(we usually say fork) one more process from the java process.
for example, java is running as a process 'A'
and if the process 'A' starts another process 'B'
then those are running at the same time.
(in your case, A is 'JAVA' and B is 'Shell')
so, it will be 2. and the 2 processes are running in the same time (parallely), so you will get two of those results at the same time.
1 Comment
p.waitFor() ; because now process A will not start until process B is finished?
echocommands to the bash script so you see how far it gets.