0

I want to execute curl command from java code. I have already seen couple of documents, stackoverflow questions regarding the same. But it is not giving the desired result, I am trying to run this curl command :

curl --noproxy <ip> 
-i 
-d "INSERT IN GRAPH <http://graph.com>{ <prop/Advanced_Data_Types> rdf:type owl:NamedIndividual ,
                                    <http://abcd/Video> ,
                                   <http://abcd/LearningResource/BookChapter> ;
                           <http://abcd/hasAuthor> <prop/Eric_Grimson> ;
                           <http://abcd/inLanguage> <http://abcd/#Hindi> ;
                           <http://abcd/sourceOrganization> <prop/IIT_Hyderabad> .}" 
-u "demo:demo" 
-H "Content-Type: application/sparql-query" http://<ip>:<port>/DAV/home/dba/xx

And the JAVA code :

ProcessBuilder pb = new ProcessBuilder(
            "curl",
            "-i",
            "-d \"INSERT IN GRAPH <http://graph.com>{ <prop/Advanced_Data_Types> rdf:type owl:NamedIndividual ,<http://abcd/Video> ,<http://abcd/LearningResource/BookChapter> ;<http://abcd/hasAuthor> <prop/Eric_Grimson> ;<http://abcd/inLanguage> <http://abcd/#Hindi> ;<http://abcd/sourceOrganization> <prop/IIT_Hyderabad> .} ",
            "-u \"demo:demo\"",
            "-H \"Content-Type: application/sparql-query\"",
            "http://<ip>:<port>/DAV/home/dba/xx");

    pb.redirectErrorStream(true);
    Process p = pb.start();
    InputStream is = p.getInputStream();

    String line;
    BufferedInputStream bis = new BufferedInputStream(is);
    System.out.println(bis);

Is anything wrong in my code? I want to use this particular curl command. Please help.

2
  • 1
    A space on the command line means a new argument. Commented Mar 17, 2017 at 7:12
  • Thanks. It works. Commented Mar 17, 2017 at 7:31

2 Answers 2

1

I used it like this that executing curl in java.

    public static String invokeCurlGet(String _host, int _connectTimeout, int _maxTimeout, int _maxResLength, Charset _charset) throws IOException
    {
        byte[] res = execute("curl --connect-timeout " + _connectTimeout + " --max-time " + _maxTimeout + " -X GET " + _host, _maxResLength);

        return new String(res, _charset);
    }

    public static byte[] execute(String _cmd, int _maxResLength) throws IOException
    {
        Process process = Runtime.getRuntime().exec(_cmd);

        try
        {
            int result = process.waitFor();
            if(result != 0)
            {
                throw new IOException("Fail to execute cammand. Exit Value[" + result + "], cmd => " + _cmd);
            }
        }
        catch(InterruptedException e)
        {
            process.destroyForcibly();

            throw new IOException(e);
        }

        BufferedInputStream in = null;

        try
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            in = new BufferedInputStream(process.getInputStream());
            byte[] buf = new byte[1024];
            int read = 0;

            while((read = in.read(buf)) != -1)
            {
                out.write(buf, 0, read);
                out.flush();

                if(_maxResLength > 0 && out.size() > _maxResLength)
                {
                    throw new IOException("Response length exceeded.");
                }
            }

            return out.toByteArray();
        }
        finally
        {
            if(in != null)
            {
                in.close();
            }
        }
    }

    public static void main(String[] args) throws Exception
    {
        System.out.println(invokeCurlGet("http://127.0.0.1:9000?messageId=aaaaaaaa&to=asdfsefwaf", 3, 10, 0, Charset.defaultCharset()));
//      System.out.println(invokeCurlGet("https://github.com", 1, 1, 0, Charset.defaultCharset()));
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Your understanding on how to convert a command line to a list of string arguments is slightly incorrect. (This is roughly equivalent to how a Unix shell works).

Generally when a space is present on the command line it means starting a new argument. So

"-H \"Content-Type: application/sparql-query\""

should be

"-H", "\"Content-Type: application/sparql-query\""

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.