gpt4 book ai didi

java - 从 JAVA 运行 Bash 脚本文件

转载 作者:行者123 更新时间:2023-12-01 13:57:09 26 4
gpt4 key购买 nike

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

有人可以帮我吗?

最佳答案

您是否尝试过捕获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));

关于java - 从 JAVA 运行 Bash 脚本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19567338/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com