A newer version of this documentation is available.

View Latest
March 16, 2025
+ 12

Description — Build and run a starter app to validate your install of Couchbase Lite on Java
Abstract — This content provides sample code and instructions that enable you to test your Couchbase Lite for java installation.
Related Content — Install | Prerequisites | Build and Run |

Build a Getting Started App

This section explains how to validate your configured build environment by building a starter app that uses many of Couchbase Lite on Java’s more common features.

The GettingStarted app demonstrates how to use Couchbase Lite on Java. Console and Web App versions are available.

Development-type Scenarios
Ensure you added the Couchbase Lite dependency to your build.gradle file

Create, build and run a new project using the following GettingStarted.java code:


        CouchbaseLite.init();
        (1)
        // Create a database
        System.out.println("Starting DB");
        DatabaseConfiguration cfg = new DatabaseConfiguration();
        Database database = null;
        try {
            database = new Database(  "mydb", cfg);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

        (2)
        // Create a new document (i.e. a record) in the database.
        MutableDocument mutableDoc =
                new MutableDocument().setString("version", "2.0")
                        .setString("type", "SDK");

        (3)
        // Save it to the database.
        try {
            database.save(mutableDoc);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

        (4)
        // Retrieve and update a document.
        mutableDoc =
                database.getDocument(mutableDoc.getId())
                        .toMutable()
                        .setString("language", "Kotlin");
        try {
            database.save(mutableDoc);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

        (5)
        // Retrieve immutable document and log the document ID
        // generated by the database and some document properties
        Document document = database.getDocument(mutableDoc.getId());
        System.out.println( String.format("Document ID :: %s", document.getId()));
        System.out.println( String.format("Learning :: %s:", document.getString("language")));

        (6)
        // Create a query to fetch documents of type SDK.
        Query listQuery = QueryBuilder.select(SelectResult.all())
                .from(DataSource.database(database))
                .where(Expression.property("type").equalTo(Expression.string("SDK")));

        try {
            for (Result result : listQuery.execute().allResults()) {
                for ( String k : result.getDictionary(0).getKeys()) {
                    System.out.println( k + " = " + result.getDictionary(0).getString(k));

                }
//                            String.format("Number of rows :: %n",
//                                    rs.size()));
            }
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }


        //  (7)
        // OPTIONAL -- if you have Sync Gateway Installed you can try replication too
        // Create a replicator to push and pull changes to and from the cloud.
        // Be sure to hold a reference somewhere to prevent the Replicator from being GCed
        // BasicAuthenticator basAuth = new BasicAuthenticator("sync-gateway", "password".toCharArray());

        //ReplicatorConfiguration replConfig =
        //        new ReplicatorConfiguration(database,
        //                URLEndpoint( URI("ws://localhost:4984/getting-started-db")),
        //                        ReplicatorType.PUSH_AND_PULL,
        //                        basAuth);
        //
        //Replicator replicator = new Replicator(replConfig);

        // Listen to replicator change events.
        // Version using Kotlin Flows to follow shortly ...

        //replicator.addChangeListener(new ReplicatorChangeListener() {
        //             @Override
        //             public void changed(@NonNull ReplicatorChange change) {
        //                     if (change.getStatus().getError() != null) {
        //                             System.out.println( "Error code ::  ${err.code}");
        //                     }
        //             }
        //     });


        // Start replication.
        // replicator.start();

    }
}
1 Create a database
2 Create a mutable document
3 Save document to database
4 Retrieve the document as mutable and change the language to Java and save it
5 Retrieve document as immutable and log it ID
6 Query the database output count and id to log
7 Optionally, initiate a replication

On running the app, you should see the document ID and property printed to the console together with a query result showing the number of rows in the database.

This shows the document was successfully persisted to the database.

See [About the Getting Started App] for more on the app itself