从JAVA运行Bash脚本文件

时间:2013-10-24 13:28:26

标签: java bash function postgresql shell

我使用Bash脚本。我的bash脚本是

#!/bin/bash


function aa(){
   echo "Run";
   su - postgres -c "shp2pgsql -I -s 4269  /tmp/gismanager/Wards.shp ff | psql  postgis;";
   echo "Run";
   return 0;
}

aa;

当这个脚本由linux运行时结果是Run Run但是从java结果是Run

我的java代码是

public static void execShellCmd(String path) {
    try {
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(path);
        BufferedReader buf = new BufferedReader(new InputStreamReader(process.getInputStream()));
        String line = "";
        while ((line = buf.readLine()) != null) {
            System.out.println("exec response: " + line);
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

您是否尝试捕获OutputStream和ErrorStream?

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

InputStream inputStream = process.getInputStream();
InputStream errorStream = process.getErrorStream();

BufferedReader inputReader = new BufferedReader(new InputStreamReader(inputStream));
BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));