0

Hi I have an elasticsearch index which needs to deleted when a new entry of that type has inserted. I was able create a shellscript file and insert data into elasticsearch with a java appliaction as below,

public static void main(String[] args) {
    try {
        // Run the process
        /*Runtime.getRuntime().exec("cd src/resources");*/
        Process p = Runtime.getRuntime().exec("sh src/resources/test.sh");
        // Get the input stream
        InputStream is = p.getInputStream();

        // Read script execution results
        int i;
        StringBuffer sb = new StringBuffer();
        while ((i = is.read()) != -1)
            sb.append((char) i);

        System.out.println(sb.toString());

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Test.sh

curl -XDELETE http://localhost:9200/pokedex

With my java program it was possible to insert data but I can't delete, I tried to run the above command in command-line and it worked fine. What is happening here? Why is it executing in command-line and not in my program?

1
  • May you also add return code of the shell command executed by exec? Maybe shell is returning error code but you are ignoring it. Commented Sep 23, 2017 at 8:25

1 Answer 1

0

Does your Java program know what "sh" is? Have you tried fully qualifying the command:

 Process p = Runtime.getRuntime().exec("/bin/sh src/resources/test.sh");

What is the error when it doesn't run? Is it best practice to run a shell script in a Java program? Are there other ways of running commands in Java programs?

java Runtime.exec to run shell script

Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't show any error, it simply skips that part. Yeah I have tried the fully qualified command as well. -XPUT works fine, -XDELETE command isn't working.

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.