0

I followed these tutorials How to use cURL in Java?, https://www.baeldung.com/java-curl to learn how to run curl from Java but it is not working.

The curl command (to delete page) is running fine from the terminal and gives a response in XML, but nada when I try in java, no error, no XML, the page also remains intact.

This is what I tried:

public class TryCurl {

    static String command = "curl -u username:password -X POST -F cmd=\"deletePage\" -F path=\"/content/demo-task/section\" https://author1.dev.demo.adobecqms.net/bin/wcmcommand";

    public static void main(String[] args) throws Exception {

        Process process = Runtime.getRuntime().exec(command);
        process.getInputStream();
        process.destroy();
    }

}

What am I doing wrong here?

paul
  • 4,333
  • 16
  • 71
  • 144
  • 2
    `process.getInputStream();` you're getting it and putting it... where, exactly? – Federico klez Culloca Nov 12 '20 at 14:45
  • 2
    I suggest you read a tutorial about that Process class to learn how it works. You know, **getting** an input stream ... that returns an object from which you can read input. But you discard that object. So what do you expect this code to do? To me it seems you figured "okay, there is that Process class" ... but then you stopped researching it. – GhostCat Nov 12 '20 at 14:46
  • Can you try to print the outcome of running curl? Does process give you a way to do that? BTW, there are libraries you can just in Java to do curl-like stuff like Apache's HTTP Components – David Brossard Nov 12 '20 at 14:47
  • 2
    You don't need CURL in Java at all. HTTP supported by class [java.net.URL](https://docs.oracle.com/javase/7/docs/api/java/net/URL.html) out of the box. But you'd better use [Apache HTTP client](https://hc.apache.org/) industry standard library instead – Victor Gubin Nov 12 '20 at 14:55
  • To reiterate what others have said: Don’t use curl. Java is perfectly capable of invoking a URL and retrieving its contents. Use the URL class, or use the [java.net.http](https://docs.oracle.com/en/java/javase/15/docs/api/java.net.http/java/net/http/package-summary.html) package. (And you probably shouldn’t rely on baeldung.com for advice. I’ve observed it frequently ignoring good practices.) – VGR Nov 12 '20 at 15:14

1 Answers1

1
  1. Why are you using CURL for this; that's very 'fragile' (likely to fail in the future, it requires a lot of things to be in place, such as: curl to even be installed on the hardware you run this on, curl to be in the path, the password to not have spaces or other special characters in it, and more. Java is perfectly capable of making a POST request to a server.

  2. That's not how a single process command works. Runtime.exec is not a shell - things like: Untangle quotes into arguments are shellisms, and process doesn't do any of it. Use ProcessBuilder and pass each argument separately, no quotes.

  3. You can't just get the inputstream and discard, you need to actually read the bytes off of it, or curl will just sit there and wait for somebody to grab the data it is spitting out to standard out. This gets complicated, which brings us back to #1 which is probably a lot easier here.

  4. you immediately run process.destroy() which will, rather obviously, destroy that process, hence the name. curl will just quit, because you asked it to, before it so much as finishes sending the POST.

execing is a lot more complicated than it sounds.

rzwitserloot
  • 85,357
  • 5
  • 51
  • 72