0

我使用 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);
    }
}

有谁能够帮我?

4

1 回答 1

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));
于 2013-10-24T13:42:09.037 回答