Java Top

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial, we’re going to look at how to use the curl tool inside a Java program.

Curl is a networking tool used to transfer data between a server and the curl client using protocols like HTTP, FTP, TELNET, and SCP.

2. Basic Use of Curl

We can execute curl commands from Java by using the ProcessBuilder — a helper class for building instances of the Process class.

Let’s see an example of sending commands directly to the operating system:

String command =
  "curl -X GET https://postman-echo.com/get?foo1=bar1&foo2=bar2";
ProcessBuilder processBuilder = new ProcessBuilder(command.split(" "));

First, we create the command variable before passing it to the ProcessBuilder constructor.

It’s worth noting here that if the curl executable isn’t on our system path, we’ll have to provide its full path in our command string.

We can then set the working directory for the ProcessBuilder and start the process:

processBuilder.directory(new File("/home/"));
Process process = processBuilder.start();

From here on, we can get the InputStream by accessing it from the Process instance:

InputStream inputStream = process.getInputStream();

When the processing is complete, we can get the exit code with:

int exitCode = process.exitValue();

If we need to run additional commands, we can reuse the ProcessBuilder instance by passing new commands and arguments in a String array:

processBuilder.command(
  new String[]{"curl", "-X", "GET", "https://postman-echo.com?foo=bar"});

Finally, to terminate each Process instance, we should use:

process.destroy();

3. A Simple Alternative to the ProcessBuilder

As an alternative to using the ProcessBuilder class, we can use Runtime.getRuntime() to get an instance of the Process class.

Let’s see another sample curl command – this time using a POST request:

curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2

Now, let’s execute the command by using the Runtime.getRuntime() method:

String command = "curl -X POST https://postman-echo.com/post --data foo1=bar1&foo2=bar2";
Process process = Runtime.getRuntime().exec(command);

Firstly, we create an instance of the Process class again, but this time using Runtime.getRuntime(). We can get an InputStream as in our previous example by calling the getInputStream() method:

process.getInputStream();

When the instance is no longer needed, we should release system resources by calling the destroy() method.

4. Conclusion

In this article, we have shown two ways of using curl in Java.

This and more code examples are available over on GitHub.

Java bottom

I just announced the new Learn Spring course, focused on the fundamentals of Spring 5 and Spring Boot 2:

>> CHECK OUT THE COURSE
newest oldest most voted
Notify of
calvin
Guest
calvin

Why would any one use curl in this way?

Loredana Crusoveanu
Editor

Hi Calvin,

There may be situations where you don’t have access to a machine to run it the usual way.

Also, as you’ll be able to see in here: https://stackoverflow.com/questions/2586975/how-to-use-curl-in-java
The topic has some interest in the community and that’s why we added this post.

Cheers.

Comments are closed on this article!