Skip to content
Sample for using Cloud Spanner from App Engine Java
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.


Type Name Latest commit message Commit time
Failed to load latest commit information.

App Engine Java 11 sample


This sample demonstrates how to use Cloud Spanner from the App Engine Standard with Java 11 environment.

Please feel free to report issues and send pull requests, but note that this application is not officially supported as part of the Cloud Spanner product.


Create a project in the Google Cloud Platform Console

If you haven't already created a project, create one now.

  1. Open the Cloud Console.
  2. In the drop-down menu at the top, select Create a project.
  3. Give your project a name.
  4. Make a note of the project ID, which might be different from the project name. The project ID is used in commands below.

If you like, you can activate a Cloud Shell for your project now. When using Cloud Shell, the Google Cloud SDK and Maven in the steps below will already be installed.

Set up your Google Cloud Platform Project

Install the Google Cloud SDK if it's not already available in your enviromment. Then run:

gcloud init

If this is your first time creating an App engine application:

gcloud app create

Install Maven

This sample uses the Apache Maven build system. If Maven isn't installed in your environment yet, download and install it. When you use Maven as described here, it will automatically download the required client libraries.

Google Cloud Shell Open JDK 11 setup:

To switch to Open JDK 11 in a Cloud Shell session, run:

# Select the usr/lib/jvm/java-11-openjdk-amd64/bin/java version.
sudo update-alternatives --config java
# Set the JAVA_HOME variable for Maven to pick the correct JDK:
export JAVA_HOME=/usr/lib/jvm/java-11-openjdk-amd64

Set up the sample

Clone the repository:

git clone

Compile the simple-jetty-main Main class:

cd appengine-java-sample/simple-jetty-main
mvn install

If this fails with an error about the Java target version, make sure you've switched to the Open JDK 11 as described above.

Change to the appengine-java-sample directory:

cd ..

Next, create a Spanner instance and update the <SPANNER_INSTANCE> value in app.yaml with your instance ID.


Replace <PROJECT_ID> below with your project ID:

mvn package appengine:deploy -Dapp.deploy.projectId=<PROJECT_ID>


/spanner: will run a number of sample operations against the Spanner instance. Individual tasks can be run using the task query parameter. See SpannerTasks for the supported set of tasks.

To execute the endpoint handler of the deployed sample application, open https://<PROJECT_ID> in a web browser.

Note: by default all the Spanner example operations run in order, so this endpoint handler may take a while to return.

You can’t perform that action at this time.