0

I'm trying to run a command from 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.

3
  • -e instead of or in addition to -i ? Commented Mar 28, 2014 at 6:28
  • define "not executing", any stacktrace? Also, this will most likely make tomcat redeploy the SMS webapp Commented Mar 28, 2014 at 6:29
  • No shell => no quoting. Commented Mar 28, 2014 at 6:44

1 Answer 1

2

You could add /bin/sh -c before your command and have a try.

Like this:

String[] command = {"/bin/sh", "-c", "sed -i '/" + subscriberId + "/d' "+file};
Process p = run.exec(command);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.