[Solved] How do i execute shell script using java


This is a simple code to execute the shell script and read its output:

import java.io.*;                                                            

public class Test {                                                          
  public static void main(String[] args) throws Exception {                  
    String[] command = { "./flows.sh", "suspend" };         
    Process process = Runtime.getRuntime().exec(command);                    
    BufferedReader reader = new BufferedReader(new InputStreamReader(    
        process.getInputStream()));                                          
    String s;                                                                
    while ((s = reader.readLine()) != null) {                                
      System.out.println("Script output: " + s); // Replace this line with the code to print the result to file                     
    }                                                                        
  }                                                                          
}      

To print it to a file, just replace the System.out.println for the code to write into a file

3

solved How do i execute shell script using java