Java:Runtime.getRuntime()。exec()系统调用错误/失败

时间:2020-04-06 17:41:17

标签: java linux operating-system system

我有一个shell脚本,该脚本返回给定目录路径和Unix用户的磁盘使用情况。我想用java系统调用执行此命令,下面是我的代码。当我在终端中运行命令时,它可以正常运行,但是Runtime.getRuntime()。exec(cmd)给出以下错误。您知道导致此错误的原因是什么吗?

终端(有效):> usr / bin /状态存储--format json“ \” path ='/ a / b / c / d'&& User ='testuser'

错误: 行:未定义参数“ path ='/ a / b / c / d' 出口:234

String s;
Process p;
try {
    String cmd = "/usr/bin/status storage --format json \"path='/a/b/c/d' && User='testuser'\"";
    System.out.println(cmd); //running printed output in terminal works, not in exec() call
    p = Runtime.getRuntime().exec(cmd);
    BufferedReader br = new BufferedReader(
        new InputStreamReader(p.getInputStream()));
    while ((s = br.readLine()) != null)
        System.out.println("line: " + s);
    p.waitFor();
    System.out.println ("exit: " + p.exitValue());
    p.destroy();
} catch (Exception e) {}

0 个答案:

没有答案