Quickstart for Java 8 for App Engine Standard Environment

This quickstart shows you how to create a small App Engine application that displays a short message.

Before you begin

Before running and deploying this sample, take the following steps:

  1. Use the Cloud Platform Console to create a new Cloud Platform project, create an App Engine application:
    Go to App Engine

    When prompted, select the region where you want your App Engine application located.

  2. Install the following prerequisites
    1. Download and install git.
    2. Download and install the Google Cloud SDK:
      Download the SDK
    3. After you download and install invoke the following commands:
      gcloud init
      gcloud auth application-default login
    4. Update to the latest version of Cloud SDK and all components:
       gcloud components update 
    5. This quickstart uses Apache Maven version 3.5 or greater to build, run, and deploy the sample app. For details about installing Maven, see Using Apache Maven and the App Engine Plugin.

App Engine locations

App Engine is regional, which means the infrastructure that runs your apps is located in a specific region and is managed by Google to be redundantly available across all the zones within that region.

Meeting your latency, availability, or durability requirements are primary factors for selecting the region where your apps are run. You can generally select the region nearest to your app's users but you should consider the location of the other Cloud Platform products and services that are used by your app. Using services across multiple locations can affect your app's latency as well as pricing.

App Engine is available in the following regions:

  • us-central1 (Iowa)
  • us-east1 (South Carolina)
  • us-east4 (Northern Virginia)
  • southamerica-east1 (São Paulo) *
  • europe-west1 (Belgium)
  • europe-west2 (London)
  • europe-west3 (Frankfurt)
  • asia-northeast1 (Tokyo)
  • asia-south1 (Mumbai)
  • australia-southeast1 (Sydney)

* For customers using the São Paulo region, all regional product SLAs will remain in force. However, multi-region and cross-region functionality spanning North America and South America may temporarily have reduced availability or performance.

You cannot change an app's region after you set it.

To view your App Engine project region, go to the App Engine Dashboard in the Cloud Platform Console. The project's region is listed under http://[YOUR_PROJECT_ID].appspot.com. Alternatively, you can also run gcloud app describe on the command line from the root directory of your App Engine project to get the locationId.

This quickstart assumes that you have installed the Java SE 8 Development Kit (JDK).

Download the Hello World app

We've created a simple Hello World app for the Java 8 runtime so you can quickly get a feel for deploying an app to the App Engine standard environment.

Download the sample app and navigate into the app directory:

  1. Clone the Hello World sample app repository to your local machine:

    git clone https://github.com/GoogleCloudPlatform/getting-started-java.git
    

    Alternatively, download the sample as a zip file and extract it.

  2. Change to the directory that contains the sample code:

    cd getting-started-java/appengine-standard-java8/helloworld
    

Run Hello World on your local machine

To run the Hello World app on your local computer:

  1. Start the local Jetty web server using the Jetty Maven plugin:

    mvn appengine:run
    
  2. In your web browser, visit the following address:

    http://localhost:8080
    
  3. You can see the Hello World message from the sample app displayed in the page.

  4. In your terminal window, press Ctrl+C to exit the web server.

Deploy and run Hello World on App Engine

To deploy your app to the App Engine standard environment:

  1. Deploy the Hello World app by running the following command from the getting-started-java/appengine-standard-java8/helloworld directory:
    mvn appengine:deploy
    Wait for the deployment to finish.
  2. Launch your browser and view the app at http://YOUR_PROJECT_ID.appspot.com, by running the following command:
    gcloud app browse

This time, the page that displays the Hello World message is delivered by a web server running on an App Engine instance.

Congratulations! You've deployed your first Java 8 app to App Engine standard environment!

See the following sections for information about cleaning up as well as links to the possible next steps that you can take.

What's next

Learn the whole platform

Now that you know what it's like to develop and deploy App Engine apps, you can stretch out and see the rest of Google Cloud Platform. You already have the Google Cloud SDK installed and that gives you the tools to interact with products like Google Cloud SQL, Google Cloud Storage, Google Cloud Datastore, and more. For a guided walkthrough that teaches you how to create an application that uses the entire platform, not just App Engine, check out our quickstart on creating the Bookshelf app.

Learn about the App Engine standard environment

Here are some topics to help continue your learning about App Engine.

Hello World code review

Hello World is the simplest possible App Engine app, as it contains only one service, has only one version, and all of the code is located within the app's root directory. This section describes each of the app files in detail.

HelloServlet.java

The HelloServlet.java file specifies a URL pattern that describes where the app will listen for requests, and responds to any requests with the 'Hello World' message.

import com.google.appengine.api.utils.SystemProperty;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
@WebServlet(name = "HelloAppEngine", value = "/hello")
public class HelloAppEngine extends HttpServlet {

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws IOException {
    Properties properties = System.getProperties();

    response.setContentType("text/plain");
    response.getWriter().println("Hello App Engine - Standard using "
            + SystemProperty.version.get() + " Java "
            + properties.get("java.specification.version"));
  }

  public static String getInfo() {
    return "Version: " + System.getProperty("java.version")
          + " OS: " + System.getProperty("os.name")
          + " User: " + System.getProperty("user.name");
  }

}

pom.xml

Hello World also includes a pom.xml file, which contains information about the project like its dependencies and the build target, including the required lines for using the Maven plugin:

<plugin>
  <groupId>com.google.cloud.tools</groupId>
  <artifactId>appengine-maven-plugin</artifactId>
  <version>1.3.1</version>
</plugin>

appengine-web.xml

The appengine-web.xml file contains certain required settings, such as the java8 setting for the <runtime> element that tells App Engine to run this application in the Java 8 runtime:

<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
  <runtime>java8</runtime>
  <threadsafe>true</threadsafe>
</appengine-web-app>

外出先でもリソースをモニタリング

Google Cloud Console アプリを入手して、プロジェクトの管理にお役立てください。

フィードバックを送信...

App Engine standard environment for Java