Java

Java

Edit on GitHub
Platform: iOS Android WPF

Add Couchbase Lite to your app

Maven

  1. Open pom.xml in the IDE of your choice.
  2. Add the following inside the dependencies XML tag.

    <dependency>
            <groupId>com.couchbase.lite</groupId>
            <artifactId>couchbase-lite-java</artifactId>
            <version>1.4.1</version>
    </dependency>
    
  3. Sync the dependencies and start using Couchbase Lite in your project.

Available artifacts

The following libraries can be used in a Java project to import different components.

Artifact name Service
couchbase-lite-java-listener To access the database through HTTP (often used for hybrid development and peer-to-peer sync).
couchbase-lite-java-forestdb To use ForestDB as the storage type.
couchbase-lite-java-sqlcipher To enable encryption on SQLite databases.
couchbase-lite-java-javascript JavaScript view (map/reduce) engine for the REST API.

Other options

You can find instructions for other build systems (Gradle etc.) on the Maven repository page.

Getting Started

Create a new command line app in the IDE of your choice (in this example we'll use IntelliJ IDEA CE). By default, new projects in IntelliJ are not setup to download dependencies from Maven. Right-click on the project and select the Add Framework Support menu.

In the pop-up window, select Maven as the build system to add it to the project.

This step will create a new pom.xml file. Add the following inside the <project> XML tag.

<dependencies>
        <dependency>
                <groupId>com.couchbase.lite</groupId>
                <artifactId>couchbase-lite-java</artifactId>
                <version>1.4.1</version>
        </dependency>
</dependencies>

Open Main.java and add the following in the main method.

// Enable logging
Logger log = Logger.getLogger("app");
log.setLevel(Level.ALL);
JavaContext context = new JavaContext();
// Create a manager
Manager manager = null;
try {
    manager = new Manager(context, Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
    e.printStackTrace();
}
// Create or open the database named app
Database database = null;
try {
    database = manager.getDatabase("app");
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
// The properties that will be saved on the document
Map<String, Object> properties = new HashMap<String, Object>();
properties.put("title", "Couchbase Mobile");
properties.put("sdk", "Java");
// Create a new document
Document document = database.createDocument();
// Save the document to the database
try {
    document.putProperties(properties);
} catch (CouchbaseLiteException e) {
    e.printStackTrace();
}
// Log the document ID (generated by the database)
// and properties
log.info(String.format("Document ID :: %s", document.getId()));
log.info(String.format("Learning %s with %s", (String) document.getProperty("title"), (String) document.getProperty("sdk")));

// Create replicators to push & pull changes to & from Sync Gateway.
URL url = null;
try {
        url = new URL("http://localhost:4984/hello");
} catch (MalformedURLException e) {
        e.printStackTrace();
}
Replication push = database.createPushReplication(url);
Replication pull = database.createPullReplication(url);
push.setContinuous(true);
pull.setContinuous(true);

// Start replicators
push.start();
pull.start();

Build and run. Notice the document ID and property are printed to the console. The document was successfully persisted to the database.

Installing Sync Gateway ⟶