The Java 8 Runtime

The Java 8 runtime does not include any web-serving framework. The only requirement is that your app should listen and respond on port 8080. The sample code here shows how you can add your own framework, such as SparkJava or Spring Boot, to the Java 8 runtime.

Prerequisites

Before you start developing, follow these steps:

Adding the App Engine Maven plugin

To deploy using maven, you will need to add the appengine-maven-plugin to your pom.xml:

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

Other options for deploying include using the gcloud app deploy command or the App Engine Gradle plugin.

The Maven assembly plugin

Most apps will require adding the Maven assembly plugin to their pom.xml which creates a jar with dependencies. You may also create a Fat Jar.

<plugin>
  <artifactId>maven-assembly-plugin</artifactId>
  <executions>
    <execution>
      <phase>package</phase>
      <goals>
        <goal>single</goal>
      </goals>
    </execution>
  </executions>
  <configuration>
    <descriptorRefs>
      <descriptorRef>jar-with-dependencies</descriptorRef>
    </descriptorRefs>
    <archive>
      <manifest>
        <mainClass>com.google.appengine.sparkdemo.Main</mainClass>
      </manifest>
    </archive>
  </configuration>
</plugin>

Note that you need to identify the main class to the plugin, in this case com.google.appengine.sparkdemo.Main

Organizing your files

Your development file hierarchy should look like this:

MyDir/
  pom.xml
  [index.yaml]
  [cron.yaml]
  [dispatch.yaml]
  src/main/
    appengine/
      app.yaml
      Dockerfile
    java/
      com.example.mycode/
        MyCode.java

Required files

app.yaml

You'll need to define an app.yaml file that looks like this:

runtime: custom
env: flex

Other app.yaml settings are described in Using app.yaml.

JAR file

You must create a JAR file for your app. Since this runtime contains only Open JDK 8, you must install your own web serving framework, such as Spring Boot or Spark. Your app will listen to HTTP port 8080.

Dockerfile

You will also need to add a Dockerfile to src/main/appengine/. The file must include the base image gcr.io/google_appengine/openjdk8. This example project shows how to use the ADD directive to add your build jar as app.jar, and the CMD directive to set the arguments for the java command.

FROM gcr.io/google_appengine/openjdk8
VOLUME /tmp
ADD spark-1.0-jar-with-dependencies.jar app.jar
CMD [ "java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar"]

Optional files

The additional configuration files index.yaml, cron.yaml, and dispatch.yaml are optional. They are placed at the top level of MyDir. If you use any these files they must be deployed separately with the gcloud app deploy command.

Environment variables

Use the env_variables key in the app.yaml file to set environment variables:

env_variables:
   MY_VAR_COLOR: 'blue'

Local Testing

You can build your app using:

mvn package

And run it using the java command line:

java -jar target/myjar.jar

Or, you can add the Maven Exec plugin to your pom.xml.

<plugin>
  <groupId>org.codehaus.mojo</groupId>
  <artifactId>exec-maven-plugin</artifactId>
  <version>1.4.0</version>
  <configuration>
    <mainClass>com.google.appengine.sparkdemo.Main</mainClass>
    <arguments>
      <argument>-jar</argument>
      <argument>target/spark-1.0-jar-with-dependencies.jar</argument>
    </arguments>
  </configuration>
</plugin>

Note that you need to give both the main class (in this case com.google.appengine.sparkdemo.Main) and the path to the jar (in this case target/sparkdemo-1.0-jar-with-dependencies.jar). Then execute using the Maven command:

mvn exec:java

When you are testing in your local environment, you might prefer to use emulated Google cloud services, rather than remote Google cloud services. There are emulators for Cloud Datastore, Cloud PubSub, and Cloud Bigtable. Use the gcloud command to start them before you run your app:

gcloud beta emulators datastore start
gcloud beta emulators pubsub start
gcloud beta emulators bigtable start

Deploying your app

Use Maven to deploy your app:

mvn appengine:deploy

Alternatively, you can deploy either a Fat jar or war file, by placing them in a directory with an app.yaml as described above.

gcloud app deploy

If you are using any of the optional configuration files (index.yaml, cron.yaml, and dispatch.yaml) be sure to deploy them separately with the gcloud command, for example:

gcloud app deploy cron.yaml

Customizing the Java 8 runtime

You don't need a Dockerfile to deploy your app into the Java 8 runtime. If your app requires additional configuration, you can explicitly provide a Dockerfile to customize the Java runtime. The Dockerfile must include the base image gcr.io/google_appengine/openjdk8. You can add additional directives to the Dockerfile to customize the Java runtime. See Building Custom Runtimes.

Send feedback about...

App Engine flexible environment for Java docs