Categories
curl java

How to use cURL in Java?

108

I am a newbie in java and wanted to use curl in java. What is my question is curl built-in in java or I have to install it from any 3rd party source to use with Java. If so, how to install curl in java. I have been googling for a long time but didnt find any help. Hope anyone can help me out there.

Thanks in advance.

3

  • @skaffman: Nothing yet. I am a PHP Programmer who is recently trying java. So naturally I am very used to curl. So, while working on java as a novice, this question comes up in my mind, still out of curiosity but I am sure in near future I will need it. If anyone knows any better alternative of curl in java, then please also share. It will be very very helpful. Thanks.

    – moshfiqur

    Apr 6, 2010 at 17:47

  • 1

  • I ask because CURL does a whole bunch of stuff, some of which is built-in to Java, some of which isn’t. More specific requirements would help a lot here.

    – skaffman

    Apr 6, 2010 at 18:00

146

You can make use of java.net.URL and/or java.net.URLConnection.

URL url = new URL("https://stackoverflow.com");

try (BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream(), "UTF-8"))) {
    for (String line; (line = reader.readLine()) != null;) {
        System.out.println(line);
    }
}

Also see the Oracle’s simple tutorial on the subject. It’s however a bit verbose. To end up with less verbose code, you may want to consider Apache HttpClient instead.

By the way: if your next question is “How to process HTML result?”, then the answer is “Use a HTML parser. No, don’t use regex for this.”.

See also:

9

  • @BalusC: Thanks. Doing some studies from the links you provided. btw, do you know anything better alternative of curl in java, that will provide same functionality like curl? Thanks again.

    – moshfiqur

    Apr 6, 2010 at 17:51

  • 7

    As mentioned, HttpClient is “the better alternative”. You can in fact do everything with URLConnection, you yet has to know and understand the HTTP specs. By the way, don’t try to compare PHP with Java. It’s like apples and oranges. To avoid self-troubles/confusions, rather forget about PHP and put a fresh view on Java. It’s a language with an entirely different ideology. PS: I do both, so I tell from experience.

    – BalusC

    Apr 6, 2010 at 17:54


  • As this and other people have mentioned use HttpClient. I’d really recommend avoiding using libcurl, you have to make sure you bundle the native library for every platform you want to support and sooner or later someone will be complaining that it doesn’t work on their old power mac or SGI workstation.

    – vickirk

    Apr 6, 2010 at 19:34

  • don’t you think, catch block always looks nice with try block 😀

    Apr 23, 2015 at 6:37

  • Ahh… now i got it. you can use this type of code where we will need nested try/catch. sorry my mistake!

    Apr 23, 2015 at 12:20

5

Some people have already mentioned HttpURLConnection, URL and URLConnection. If you need all the control and extra features that the curl library provides you (and more), I’d recommend Apache’s httpclient.

    4

    The Runtime object allows you to execute external command line applications from Java and would therefore allow you to use cURL however as the other answers indicate there is probably a better way to do what you are trying to do. If all you want to do is download a file the URL object will work great.