简体   繁体   English

从java运行Linux命令

[英]Running a Linux command from java

I'm trying to run a command from java. 我正在尝试从java运行命令。 But command is not executing. 但命令没有执行。 Can anyone pointout why? 任何人都可以指出为什么?

 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;
  }

In the log it is printing the command correctly. 在日志中,它正确地打印命令。

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

But it is not executing. 但它没有执行。

You could add /bin/sh -c before your command and have a try. 您可以在命令之前添加/bin/sh -c并尝试一下。

Like this: 像这样:

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

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

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