Quickstart for App Engine

This page shows you how to deploy an Java App Engine application and use Debugger to debug an error in the application.

Before you begin

  1. Sign in to your Google account.

    If you don't already have one, sign up for a new account.

  2. Select or create a Cloud Platform project.

    Go to the Manage resources page

  3. Enable billing for your project.

    Enable billing

  4. Make sure that the following software is installed on your local system:

Deploy an App Engine app

In this Quickstart, you'll debug a simple Java App Engine application that detects the user's browser and prints a message like "Hello Firefox" or "Hello Chrome" accordingly. The source code for the application is in the GitHub project cloud-debugger-idea-sample.

Note: The application contains a bug that results in the wrong browser name being displayed in Google Chrome. You'll use Debugger to track down and fix the bug.

  1. Clone the project to a local repository:

    git clone https://github.com/GoogleCloudPlatform/cloud-debugger-idea-sample.git

    Git returns the following message:

    Cloning into 'cloud-debugger-idea-sample'...
    remote: Counting objects: 108, done.
    remote: Total 108 (delta 0), reused 0 (delta 0), pack-reused 108
    Receiving objects: 100% (108/108), 31.41 KiB | 0 bytes/s, done.
    Resolving deltas: 100% (30/30), done.
  2. In your local copy of the source, open the file pom.xml in a text editor and change the artifactId and app.id elements from hellobrowser to the project name you registered in the Cloud Platform Console.

  3. Build and test the application. From the /cloud-debugger-idea-sample/ directory, run:

    $ mvn clean install

    Maven returns the following response:

    [INFO] ------------------------------------------------------------------------
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time: 38.141 s
    [INFO] Finished at: 2016-03-17T14:21:50-07:00
    [INFO] Final Memory: 21M/181M
    [INFO] ------------------------------------------------------------------------
  4. Commit your changes and include a description:

    git commit -a -m "Setting project ID."
  5. Initialize the Google Cloud SDK with your project ID. If prompted, you can decline to clone a Git repository:

    gcloud init

    The gcloud command-line tool prompts for some configuration options:

    Pick configuration to use:
    [1] Re-initialize this configuration [default] with new settings
    [2] Create a new configuration
    Please enter your numeric choice:  2
    Enter configuration name:  debugger-sample
    Your current configuration has been set to: [debugger-sample]
    Pick credentials to use:
     [1] example@gmail.com
     [2] Login with new credentials
    Please enter your numeric choice:  1
    You are now logged in as: [example@gmail.com]
    Pick cloud project to use:
     [1] [debugger-sample]
    Please enter your numeric choice:  1
    Your current project has been set to: [debugger-sample].
  6. Authorize Git to access Google Cloud Platform. On Linux and Mac OS X:

    git config credential.helper gcloud.sh

    On Windows:

    git config credential.helper gcloud.cmd
  7. Add Cloud Source Repositories as a remote for the project:

    gcloud beta source repos create [YOUR_REPO]
    git remote add google https://source.developers.google.com/p/[PROJECT_ID]/r/[YOUR_REPO]

    where [PROJECT_ID] is the ID of your project.

  8. Commit and push your source code to the Cloud Source Repository associated with your project:

    git push --all google

    Git returns the following:

    Counting objects: 30, done.
    Delta compression using up to 4 threads.
    Compressing objects: 100% (17/17), done.
    Writing objects: 100% (30/30), 2.11 KiB | 0 bytes/s, done.
    Total 30 (delta 7), reused 0 (delta 0)
    remote: Storing objects: 100% (30/30), done.
    remote: Processing commits: 100% (3/3), done.
    To https://source.developers.google.com/p/*projectname*/
       530f08f..a5e90b0  master -> master
  9. Deploy your application using Maven:

    mvn appengine:update

    Maven returns the following message once your application has been successfully deployed:

    Update for module default completed successfully.

Try your app

Visit your application at http://[PROJECT_ID].appspot.com/hellobrowser using Google Chrome:

Hello Firefox

That's a bug. Notice that the greeting says "Hello, Firefox" even though you are viewing the page in Chrome. This is a bug in the application that you'll investigate and fix in the steps below.

Open the debug view

  1. Navigate to the Debug page in the Google Cloud Platform Console.

Set a snapshot location

The sample application uses the value of the userAgent string in the browser request to determine the type of web browser. Set a snapshot location to view the value of this string when a user opens the application URL:

  1. In the file tree, expand src > main > java/com/google/gct/tutorial/.

  2. Select HelloBrowserServlet.java. The file's contents are displayed.

  3. Click line 31 in the gutter. A blue arrow appears, indicating that a snapshot location is set. The results pane at the right of the screen displays "Waiting for snapshot to hit."

    A snapshot location set on line 31

  4. Reload http://projectname.appspot.com/hellobrowser. The results pane displays local variables and method arguments in scope at that point in the code and their values at that point in time.

    Results pane with local variables

Analyze the results

The results pane shows the value of userAgent. In this case, the value is:

Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3)
AppleWebKit/537.36 (KHTML, like Gecko)
Chrome/49.0.2623.87 Safari/537.36

There's our bug! Chrome is sending a user agent string that contains the words "Mozilla" and "Safari" (This stackoverflow.com question has details on why.)

Fix the bug

  1. In your local copy of the source, open the HelloBrowserServlet.java file in your favorite text editor:

    pico src/main/java/com/google/gct/tutorial/HelloBrowserServlet.java
  2. Update the line targeting "Mozilla":

    if (userAgent.contains("Mozilla")) {

    to read:

    if (userAgent.contains("Firefox")) {
  3. Update the line containing "Safari":

    } else if (userAgent.contains("Safari")) {

    to read:

    } else if (userAgent.contains("Safari") && !userAgent.contains("Chrome")) {
  4. Save the file.

  5. Deploy the updated code to App Engine:

    $ mvn appengine:update
  6. Reload your application at http://projectname.appspot.com/hellobrowser.

    Hello Chrome

    Hello Chrome!

Clean up

To avoid incurring charges to your Google Cloud Platform account for the resources used in this quickstart:

  1. Navigate to the Projects page of the Cloud Platform Console.

  2. Click the trash can icon () next to your project.

  3. Type your project name to confirm the shutdown.

What's next

Monitor your resources on the go

Get the Google Cloud Console app to help you manage your projects.

Send feedback about...

Stackdriver Debugger Documentation