1

I have a java app in which I'm just reading a file and inserting the data into the elasticsearch index.

Here is the code I 'm using.

        in = new BufferedReader(new FileReader("file.txt"));
        line = in.readLine();
        while ((line = in.readLine()) != null) {
            System.out.println("Command: curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'");
            p = Runtime.getRuntime().exec("curl -XPOST 'http://localhost:9200/esensor/data' -d '{ \"temp\": \""+line+"\" }'");
            p.waitFor();

            BufferedReader err = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            while ((line = err.readLine()) != null) {
                 System.out.println(line);
            }
            break;
        }
        in.close();

But this is not working. Curl is giving this error.

curl: (1) Protocol 'http not supported or disabled in libcurl
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: "temp"

  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (6) Could not resolve host: "17.35757"
curl: (3) [globbing] unmatched close brace/bracket in column 1

But see I have printed the command which I'm executing. If I manually copy paste that in console then everything working fine. Why it is happening? What I'm doing wrong?

Jayadratha Mondal
  • 759
  • 1
  • 10
  • 21
  • Why don't you use the elasticsearch's client? [ElasticSearch Client](https://www.elastic.co/guide/en/elasticsearch/client/java-api/current/client.html) – chengpohi Jun 12 '15 at 05:06
  • related: http://stackoverflow.com/questions/16486649/shell-bash-brace-expansion-with-javas-runtime-exec –  Jun 12 '15 at 05:11
  • @RC how it is related to my question?? – Jayadratha Mondal Jun 12 '15 at 05:24
  • @chengpohi I was just trying to do using exec. Cause using client will cause more memory consumption. In my case that chunk of code may run in 1000-10000 threads or more. So I was thinking if exec only may cause less memory consumption. May be I'm wrong. Is it not possible with runtime.exec? – Jayadratha Mondal Jun 12 '15 at 05:26
  • I think you issue is braces `{}` interpretation try escaping them –  Jun 12 '15 at 05:30
  • @RC \{ is meaning less. Giving error. Invalid escape sequence – Jayadratha Mondal Jun 12 '15 at 05:37

1 Answers1

0

A key is in the error message from curl:

curl: (1) Protocol 'http not supported or disabled in libcurl

The single quote before the protocol name there shows that curl gets that passed as part of the argument, and thus it doesn't work the way you want it to and instead it gives curl a malformed URL.

Daniel Stenberg
  • 54,736
  • 17
  • 146
  • 222
  • Can you please give me the correct command which I have to send in runtime.exec?? Please I cant find anything working. – Jayadratha Mondal Jun 12 '15 at 08:52
  • Runtime.getRuntime().exec clearly does not support regular shell-style quoting. You better read up the details. I don't know it, I'm just interpreting what curl said... – Daniel Stenberg Jun 12 '15 at 09:00