英文:
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) {}
专注分享java语言的经验与见解,让所有开发者获益!
评论