简体   繁体   English

无法从Java程序运行的Shell脚本获取输出

[英]unable get output from a shell script running from java program

I am executing a shell script from a java program, in that shell script I am connecting to a datamart server and executing the resmgr command but I am unable to get output of resmgr command, If I run the same command from command line I am able getting the output. 我正在从Java程序执行Shell脚本,在该Shell脚本中,我正在连接到Datamart服务器并执行resmgr命令,但是我无法获取resmgr命令的输出,如果我从命令行运行相同的命令,我可以得到输出。

Shell Script: 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 Program: 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);
        }
    }
}

I guess that's because the String cmd is not interpreted by run.exec() as you expect. 我猜这是因为String cmd并未按您期望的那样由run.exec()解释。 Try changing your string cmd to an array of Strings. 尝试将您的字符串cmd更改为字符串数组。 ie 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 并检查脚本是否在当前工作目录中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM