从java运行Linux命令

从java运行Linux命令,java,linux,Java,Linux,我正在尝试从java运行一个命令。但命令并没有执行。有人能指出原因吗 String path = dest+"/info_updated.csv"; File file = new File(path); String command = "sed -i '/" + subscriberId + "/d' "+file; System.out.println("command "+command); Runtime run = Runtime.getR

我正在尝试从java运行一个命令。但命令并没有执行。有人能指出原因吗

 String path = dest+"/info_updated.csv";
     File file = new File(path);
    String command = "sed -i '/" + subscriberId + "/d' "+file;
    System.out.println("command "+command);

       Runtime run = Runtime.getRuntime();

          Process p = null;
          try {
            p = run.exec(command);

            InputStream errorStream = p.getErrorStream();
            p.waitFor();

          } catch (IOException ioe) {
            ioe.printStackTrace();
            System.out.println("ERROR.RUNNING.CMD");
          } catch (Exception e) {
            e.printStackTrace();
          } finally {
            p.destroy();
          }
          return true;
  }
在日志中,它正在正确打印命令

command sed -i '/L13876110226750000/d' /usr/share/apache-tomcat-6.0.37/webapps/SMS/info_updated.csv

但是它没有执行。

您可以在命令之前添加
/bin/sh-c
,然后再试一次

像这样:

String[] command = {"/bin/sh", "-c", "sed -i '/" + subscriberId + "/d' "+file};
Process p = run.exec(command);

-e
代替或补充
-i
?定义“不执行”,任何堆栈跟踪?此外,这很可能会使tomcat重新部署SMS webappNo shell=>no quoting。