>Iḿ 试图通过 java 代码构建一个收集我的 postgres 数据库中所有表的应用程序。
Process p1 = Runtime.getRuntime().exec("su - postgres -c "psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \$1}'"");
我在代码中过滤掉了一些表(foo,bar),我只想收集表名。
此命令直接在终端中运行没有错误,但不适用于我的应用程序!
完整代码:
Process p1 = Runtime.getRuntime().exec("su - postgres -c "psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \$1}'"");
BufferedReader in = new BufferedReader(
new InputStreamReader(p1.getInputStream()));
String line = null;
String result = "";
while ((line = in.readLine()) != null) {
System.out.println(line);
result += line;
}
我没有错误...但结果字符串为空
有人可以帮助我吗?
提前谢谢你
可执行文件中不能有双引号,因为 Java 不会正确解释它。像这样尝试一下,使用这个版本的 Runtime.exec:
String[] myCommand = new String[] {"su", "- postgres", "-c" , "psql -p 8080 -l -t | awk '! (/^ / || /^ foo/ || /^ bar/) {print \$1}" };
Process p = Runtime.getRuntime().exec(myCommand);