Cloud Foundry Java Client Library
Page last updated:
You can use the Cloud Foundry Java Client Library to manage an account on a Cloud Foundry instance.
The Cloud Foundry Java Client Library is available in the Cloud Foundry Java Client Library repository on GitHub. The library uses a reactive design based on Project Reactor.
Adding the Java Client Library
To obtain the correct components, see the Cloud Foundry Java Client Library repository on GitHub.
Most projects need two dependencies: the Operations API and an implementation of the Client API. For more information about how to add the Cloud Foundry Java Client Library as dependencies to a Maven or Gradle project, see the sections below.
Maven
Add the cloudfoundry-client-reactor and cloudfoundry-operations dependencies to your pom.xml as follows:
<dependencies>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-client-reactor</artifactId>
<version>VERSION</version>
</dependency>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-operations</artifactId>
<version>VERSION</version>
</dependency>
...
</dependencies>
Where VERSION is the latest release of the library, for example 5.18.0.RELEASE. The artifacts are available in Maven Central.
Gradle
Add the cloudfoundry-client-reactor and cloudfoundry-operations dependencies to your build.gradle file as follows:
dependencies {
implementation 'org.cloudfoundry:cloudfoundry-client-reactor:VERSION'
implementation 'org.cloudfoundry:cloudfoundry-operations:VERSION'
...
}
Where VERSION is the latest release of the library, for example 5.18.0.RELEASE. The artifacts are available in Maven Central.
Sample code
The following is a simple sample app that connects to a Cloud Foundry instance, logs in, and displays some information about a space. When running the program, provide the Cloud Foundry API host (for example, api.example.com), a valid user name and password, and the org and space as command-line parameters.
import org.cloudfoundry.operations.CloudFoundryOperations;
import org.cloudfoundry.operations.DefaultCloudFoundryOperations;
import org.cloudfoundry.reactor.DefaultConnectionContext;
import org.cloudfoundry.reactor.client.ReactorCloudFoundryClient;
import org.cloudfoundry.reactor.tokenprovider.PasswordGrantTokenProvider;
public final class JavaSample {
public static void main(String[] args) {
String apiHost = args[0];
String username = args[1];
String password = args[2];
String organization = args[3];
String space = args[4];
DefaultConnectionContext connectionContext = DefaultConnectionContext.builder()
.apiHost(apiHost)
.build();
PasswordGrantTokenProvider tokenProvider = PasswordGrantTokenProvider.builder()
.username(username)
.password(password)
.build();
ReactorCloudFoundryClient cloudFoundryClient = ReactorCloudFoundryClient.builder()
.connectionContext(connectionContext)
.tokenProvider(tokenProvider)
.build();
CloudFoundryOperations operations = DefaultCloudFoundryOperations.builder()
.cloudFoundryClient(cloudFoundryClient)
.organization(organization)
.space(space)
.build();
System.out.printf("%nSpaces:%n");
operations.spaces().list()
.doOnNext(s -> System.out.printf(" %s%n", s.getName()))
.blockLast();
System.out.printf("%nApplications:%n");
operations.applications().list()
.doOnNext(a -> System.out.printf(" %s\t(%s)%n", a.getName(), a.getRequestedState()))
.blockLast();
System.out.printf("%nServices:%n");
operations.services().listInstances()
.doOnNext(s -> System.out.printf(" %s\t(%s)%n", s.getName(), s.getService()))
.blockLast();
connectionContext.dispose();
}
}
For more details about the Cloud Foundry Java Client Library, see the Cloud Foundry Java Client Library repository on GitHub.
To view the operations and objects that you can use, see the cloudfoundry-operations Javadoc.
Create a pull request or raise an issue on the source for this page in GitHub