Re-package a WAR file into a JAR file

This document describes how to repackage a Java 8 application as a JAR file to run on the Java 11/17 runtimes.

Your application must have a Main class that starts a web server which responds to HTTP requests on the port specified by the PORT environment variable, typically 8081.

For example:

import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.webapp.Configuration.ClassList;
import org.eclipse.jetty.webapp.WebAppContext;

/** Simple Jetty Main that can execute a WAR file when passed as an argument. */
public class Main {

  public static void main(String[] args) throws Exception {
    if (args.length != 1) {
      System.err.println("Usage: need a relative path to the war file to execute");
    System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StrErrLog");
    System.setProperty("org.eclipse.jetty.LEVEL", "INFO");

    // Create a basic Jetty server object that will listen on port defined by
    // the PORT environment variable when present, otherwise on 8080.
    int port = Integer.parseInt(System.getenv().getOrDefault("PORT", "8080"));
    Server server = new Server(port);

    // The WebAppContext is the interface to provide configuration for a web
    // application. In this example, the context path is being set to "/" so
    // it is suitable for serving root context requests.
    WebAppContext webapp = new WebAppContext();
    ClassList classlist = ClassList.setServerDefault(server);

    // Enable Annotation Scanning.

    // Set the the WebAppContext as the ContextHandler for the server.

    // Start the server! By using the server.join() the server thread will
    // join with the current thread. See
    // ""
    // for more details.

WAR migration example

The following instructions demonstrate how to repackage an App Engine Java 8 hello-world application as a JAR to run on the Java 11/17 runtime.

The migration uses the appengine-simple-jetty-main artifact. This provides a Main class with a simple Jetty web server that loads a WAR file and packages your app into an executable JAR file:

  1. Clone the Embedded Jetty Server artifact to your local machine:

    git clone

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

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

    cd java-docs-samples/appengine-java11/appengine-simple-jetty-main/
  3. Install the dependency locally:

    mvn install
  4. Add the following code to the pom.xml file:

    • appengine-simple-jetty-main dependency:
    • maven-dependency plugin:
      App Engine deploys files located in the ${}/appengine-staging directory. By adding the maven-dependency plugin to your build, App Engine installs your specified dependencies to the correct folder.
  5. Create an entrypoint element in your app.yaml file to call the appengine-simple-jetty-main object and pass your WAR file as an argument. The WAR version must be same as the version listed in your pom.xml file:

    runtime: java
    env: flex
      operating_system: ubuntu18
      runtime_version: 11
    entrypoint: 'java -cp "*" com.example.appengine.jetty.Main helloworld-1.war'
    - url: /.*
      script: this field is required, but ignored
      instances: 1
  6. To run your application locally:

    1. Package your application:

      mvn clean package
    2. Start the server with your WAR file as an argument.

      For example, you can start the server in the helloworld-war sample by running the following command from your java-docs-samples/appengine-java11/appengine-simple-jetty-main/ folder:

      mvn exec:java -Dexec.args="../flexible/java-11/helloworld-war/target/helloworld-1.war"
    3. In your web browser, enter the following address:


  7. To deploy your application using Maven plugin:

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

    Replace PROJECT_ID with the ID of your Google Cloud project. If your pom.xml file already specifies your project ID, you don't need to include the -Dapp.deploy.projectId property in the command you run.