97

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.

user2864740
  • 54,112
  • 10
  • 112
  • 187
moshfiqur
  • 1,717
  • 2
  • 19
  • 21
  • @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 06 '10 at 17:47
  • 1
    Very close to http://stackoverflow.com/questions/116650/curl-equivalent-in-java – Pascal Thivent Apr 06 '10 at 17:55
  • 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 06 '10 at 18:00

6 Answers6

141

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:

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
  • @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 06 '10 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 06 '10 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 06 '10 at 19:34
  • don't you think, catch block always looks nice with try block :D – Muhammad Suleman Apr 23 '15 at 06:37
  • Ahh... now i got it. you can use this type of code where we will need nested try/catch. sorry my mistake! – Muhammad Suleman Apr 23 '15 at 12:20
  • To those whoever following the code above, make sure you have `import java.io.*;` in the import to make the code compile. – Surya Sep 13 '19 at 09:19
  • @Surya: are you also going to post the same comment in a billion of other posts which has omitted imports for brevity..? This is just so basic, everyone who has passed chapter 1 of a Java book/tutorial already knows how imports work, it's not necessary to include them in every single answer containing a Java code snippet. – BalusC Sep 13 '19 at 14:01
  • @BalusC: Yes, if needed. Assuming that everyone works on Java and passes chapter 1 book/tutorial or reading that is just bad mentoring. Someone on the internet could be just trying to fix something. :) – Surya Sep 13 '19 at 15:00
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.

Seth
  • 5,156
  • 6
  • 38
  • 53
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.

mjh2007
  • 778
  • 1
  • 8
  • 17
3

Using standard java libs, I suggest looking at the HttpUrlConnection class http://java.sun.com/javase/6/docs/api/java/net/HttpURLConnection.html

It can handle most of what curl can do with setting up the connection. What you do with the stream is up to you.

Stevko
  • 3,969
  • 6
  • 36
  • 57
3

Curl is a non-java program and must be provided outside your Java program.

You can easily get much of the functionality using Jakarta Commons Net, unless there is some specific functionality like "resume transfer" you need (which is tedious to code on your own)

Thorbjørn Ravn Andersen
  • 68,906
  • 28
  • 171
  • 323
3

Use Runtime to call Curl. This code works for both Ubuntu and Windows.

String[] commands = new String {"curl", "-X", "GET", "http://checkip.amazonaws.com"};
Process process = Runtime.getRuntime().exec(commands);
BufferedReader reader = new BufferedReader(new 
InputStreamReader(process.getInputStream()));
String line;
String response;
while ((line = reader.readLine()) != null) {
    response.append(line);
}
ariane26
  • 165
  • 1
  • 6
  • 24
  • 1
    To run this sucessfully in java 11 the string array should be initialized as below `code` String[] commands = {"curl", "-X", "GET", "http://checkip.amazonaws.com"}; `code` – ricwambugu Apr 12 '21 at 16:00