2014-04-08 188 views
0

我正在执行一个来自java程序的shell脚本,在该shell脚本中,我正在连接到数据集市服务器并执行resmgr命令,但是我无法获得resmgr的输出命令,如果我从命令行运行相同的命令,我可以得到输出。无法从运行java程序的shell脚本获取输出

Shell脚本:

#!/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); 
     } 
    } 
} 

回答

0

我想这是因为像您期望的字符串cmd不受run.exec()解释。尝试将您的字符串cmd更改为一个字符串数组。一世。即只是改变了行:

String cmd="./getDBIndex1.sh ~AP~Generic~Universal~Throughput \"Inbound Volume (octets)\"" 

此:

String[] cmd= new String[] {"./getDBIndex1.sh", "~AP~Generic~Universal~Throughput", "Inbound Volume (octets)"}; 

和刚刚离开的代码保持不变的其余部分。并检查脚本是否在当前工作目录