无法从java程序运行的shell脚本获取输出
我正在从java程序执行一个shell脚本,在该shell脚本中,我连接到datamart服务器并执行resmgr命令,但我无法获得resmgr命令的输出,如果我从命令行运行相同的命令,我可以获得输出 Shell脚本:无法从java程序运行的shell脚本获取输出,java,shell,datamart,Java,Shell,Datamart,我正在从java程序执行一个shell脚本,在该shell脚本中,我连接到datamart服务器并执行resmgr命令,但我无法获得resmgr命令的输出,如果我从命令行运行相同的命令,我可以获得输出 Shell脚本: #!/bin/bash source /opt/datamart/dataMart.env /opt/datamart/bin/resmgr -export fgp -colNames "nName frm.dbIndex" -filter "npath($1) frm.name
#!/bin/bash
source /opt/datamart/dataMart.env
/opt/datamart/bin/resmgr -export fgp -colNames "nName frm.dbIndex" -filter "npath($1) frm.name($2)" -noHead -sep ','
Java程序:
import java.io.*;
import java.util.*;
public class Test {
public static void main(String argsm[]) throws IOException, InterruptedException {
String cmd="./getDBIndex1.sh ~AP~Generic~Universal~Throughput \"Inbound Volume (octets)\"";
Runtime run = Runtime.getRuntime();
Process pr = run.exec(cmd);
pr.waitFor();
BufferedReader buf = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = "";
while ((line=buf.readLine())!=null) {
System.out.println(line);
}
}
}
我想这是因为run.exec没有像您所期望的那样解释字符串cmd。尝试将字符串cmd更改为字符串数组。我E换句话:
String cmd="./getDBIndex1.sh ~AP~Generic~Universal~Throughput \"Inbound Volume (octets)\""
为此:
String[] cmd= new String[] {"./getDBIndex1.sh", "~AP~Generic~Universal~Throughput", "Inbound Volume (octets)"};
剩下的代码保持不变。并检查脚本是否在当前工作目录中