Java: 运行时.getRuntime().exec() 系统调用错误/失败

huangapple 未分类评论47阅读模式
英文:

Java: Runtime.getRuntime().exec() system call error/Failure

问题

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) {}
英文:

I have a shell script which returns the disk usage of a given directory path and Unix user. I would like to execute this command with java system call and below is my code. When I run the command in terminal, it perfectly works, but Runtime.getRuntime().exec(cmd) gives the following error. Any idea what causing this error to happen?

Terminal (works): > usr/bin/status storage --format json ""path='/a/b/c/d' && User='testuser'

Error:
line: Parameter "path='/a/b/c/d' is not defined
exit: 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) {}

huangapple
  • 本文由 发表于 2020年4月7日 01:41:17
  • 转载请务必保留本文链接:https://java.coder-hub.com/61065695.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定