2014-04-08 86 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)"}; 

和剛剛離開的代碼保持不變的其餘部分。並檢查腳本是否在當前工作目錄