0

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

外壳脚本:

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

1 回答 1

0

I guess that's because the String cmd is not interpreted by run.exec() as you expect. Try changing your string cmd to an array of Strings. i. e. just change the line:

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

for this:

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

and just leave the rest of the code unchanged. And also check that the script is in the current working directory

于 2014-04-08T23:44:06.290 回答