linux下通过java执行sh脚本,得到返回值

具体不够充分,需要继续研究调整

public class Test {
 public static void main(final String[] args) {
  String path="/home/songjian/test.sh";
  String retval = getShellExecuted(path);
  System.out.println(retval);
 }
 
 public static String getShellExecuted(final String cmnd) {
     InputStream in = null;
     InputStreamReader insr = null;
     BufferedReader br = null;
     StringBuffer buff = new StringBuffer();
     try {
         Process process = Runtime.getRuntime().exec(cmnd);
         process.waitFor();
         in = process.getInputStream();
         insr = new InputStreamReader(in, "utf-8");
         br = new BufferedReader(insr);
         while (br.ready()) {
         String line = br.readLine();
         buff.append(line + "\r\n");
         }
     } catch (Exception e) {
     }
     return buff.toString();
     }

}

你可能感兴趣的:(linux下通过java执行sh脚本,得到返回值)