0

I have a problem with executing a command line - curl command, using a java program I have written to send data to an influxdb server.

This is the code for sending the command in my program:

public static void main (String[] args){
    String command = "curl -i -XPOST 'http://localhost:8086/write?db=speed' --data-binary 'test,dataset=perf cpu=10'";  
    execcommand (command);
}

public static void execcommand (String command){
    StringBuffer output = new StringBuffer();
    Process p;
    try {
        p = Runtime.getRuntime().exec(command);
        p.waitFor();
        BufferedReader reader =
            new BufferedReader(new InputStreamReader(p.getInputStream()));

        String line = "";
        while ((line = reader.readLine())!= null) {
            output.append(line + "\n");
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    System.out.println(output.toString().length());
}

The problem is that the data is not being send to the server, and the response from the command is zero.

The exact same command can be run from my command line, so there is nothing wrong with the syntax of the command. I'm using a macintosh if that info is useful.

My-MBP:~ MB$ curl -i -XPOST 'http://localhost:8086/write?db=speed' --data-binary 'test,dataset=perf cpu=10'
HTTP/1.1 204 No Content
Request-Id: 3f866002-3640-11e6-8988-000000000000
X-Influxdb-Version: 0.13.0
Date: Sun, 19 Jun 2016 17:07:05 GMT

My-MBP:~ MB$ 

Is the code for executing command line arguments correct? Why is the command not being executed from my java program but working fine from the terminal?

Thank you.

1 Answer 1

1

Use String[] version of Runtime.exec. So in place of

String command = "curl -i -XPOST 'http://localhost:8086/write?db=speed' --data-binary 'test,dataset=perf cpu=10'";

Create command with arguments using String[]

String[] command = {"curl", "-i", "-XPOST", "http://localhost:8086/write?db=speed", "--data-binary", "test,dataset=perf cpu=10"};

And then call exec

Runtime.getRuntime.exec(command);

Hope this helps.

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.