Permalink
Fetching contributors…
Cannot retrieve contributors at this time
158 lines (130 sloc) 5.51 KB
---
title: Cloud Foundry Java Client Library
owner: Java
---
<strong><%= modified_date %></strong>
## <a id='introduction'></a>Introduction ##
This is a guide to using the [Cloud Foundry Java Client Library](https://github.com/cloudfoundry/cf-java-client) to manage an account on a Cloud Foundry instance.
<p class="note"><strong>Note</strong>: The 1.1.x versions of the Cloud Foundry Java Client Library work with apps using Spring 4.x, and the 1.0.x versions of the Cloud Foundry Java Client Library work with apps using Spring 3.x. Both versions are available in the <a href="https://github.com/cloudfoundry/cf-java-client">source repository</a> on GitHub.</p>
## <a id='adding-the-library'></a>Adding the Library ##
Visit the [Cloud Foundry Java Client Library](https://github.com/cloudfoundry/cf-java-client) GitHub page to obtain the correct components.
Most projects need two dependencies: the Operations API and an implementation of the Client API. Refer to the following sections for more information about how to add the Cloud Foundry Java Client Library as dependencies to a Maven or Gradle project.
### <a id='maven'></a>Maven ###
Add the `cloudfoundry-client-reactor` dependency (formerly known as `cloudfoundry-client-spring`) to your `pom.xml` as follows:
```xml
<dependencies>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-client-reactor</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.cloudfoundry</groupId>
<artifactId>cloudfoundry-operations</artifactId>
<version>2.0.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
<version>2.5.0.BUILD-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-netty</artifactId>
<version>2.5.0.BUILD-SNAPSHOT</version>
</dependency>
...
</dependencies>
```
The artifacts can be found in the Spring release and snapshot repositories:
```xml
<repositories>
<repository>
<id>spring-releases</id>
<name>Spring Releases</name>
<url>http://repo.spring.io/release</url>
</repository>
...
</repositories>
```
```xml
<repositories>
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>http://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
...
</repositories>
```
### <a id='gradle'></a>Gradle ###
Add the `cloudfoundry-client-reactor` dependency to your `build.gradle` file as follows:
```
dependencies {
compile 'org.cloudfoundry:cloudfoundry-client-reactor:2.0.0.BUILD-SNAPSHOT'
compile 'org.cloudfoundry:cloudfoundry-operations:2.0.0.BUILD-SNAPSHOT'
compile 'io.projectreactor:reactor-core:2.5.0.BUILD-SNAPSHOT'
compile 'io.projectreactor:reactor-netty:2.5.0.BUILD-SNAPSHOT'
...
}
```
The artifacts can be found in the Spring release and snapshot repositories:
```
repositories {
maven { url 'http://repo.spring.io/release' }
...
}
```
```
repositories {
maven { url 'http://repo.spring.io/snapshot' }
...
}
```
## <a id='sample-code'></a>Sample Code ##
The following is a very simple sample application that connects to a Cloud Foundry instance, logs in, and displays some information about the Cloud Foundry account. When running the program, provide the Cloud Foundry target API endpoint, along with a valid user name and password as command-line parameters.
```java
import org.cloudfoundry.client.lib.CloudCredentials;
import org.cloudfoundry.client.lib.CloudFoundryClient;
import org.cloudfoundry.client.lib.domain.CloudApplication;
import org.cloudfoundry.client.lib.domain.CloudService;
import org.cloudfoundry.client.lib.domain.CloudSpace;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
public final class JavaSample {
public static void main(String[] args) {
String target = args[0];
String user = args[1];
String password = args[2];
CloudCredentials credentials = new CloudCredentials(user, password);
CloudFoundryClient client = new CloudFoundryClient(credentials, getTargetURL(target));
client.login();
System.out.printf("%nSpaces:%n");
for (CloudSpace space : client.getSpaces()) {
System.out.printf(" %s\t(%s)%n", space.getName(), space.getOrganization().getName());
}
System.out.printf("%nApplications:%n");
for (CloudApplication application : client.getApplications()) {
System.out.printf(" %s%n", application.getName());
}
System.out.printf("%nServices%n");
for (CloudService service : client.getServices()) {
System.out.printf(" %s\t(%s)%n", service.getName(), service.getLabel());
}
}
private static URL getTargetURL(String target) {
try {
return URI.create(target).toURL();
} catch (MalformedURLException e) {
throw new RuntimeException("The target URL is not valid: " + e.getMessage());
}
}
}
```
For more details about the Cloud Foundry Java Client Library, visit the
[source repository](https://github.com/cloudfoundry/cf-java-client) in GitHub.
The [domain package](https://github.com/cloudfoundry/cf-java-client/tree/master/cloudfoundry-client/src/main/java/org/cloudfoundry/client/v2/domains) shows the objects that you can query and inspect.