本文档介绍如何将 Java 8 应用重新封装为 JAR 文件,以便在 Java 11/17 运行时环境中运行。
您的应用必须具有一个 Main 类,该类会启动一个 Web 服务器以响应由 PORT 环境变量指定的端口 8080 上的 HTTP 请求。
例如:
importorg.eclipse.jetty.server.Server;importorg.eclipse.jetty.webapp.Configuration.ClassList;importorg.eclipse.jetty.webapp.WebAppContext;/** Simple Jetty Main that can execute a WAR file when passed as an argument. */publicclassMain{publicstaticvoidmain(String[]args)throwsException{if(args.length!=1){System.err.println("Usage: need a relative path to the war file to execute");System.exit(1);}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.intport=Integer.parseInt(System.getenv().getOrDefault("PORT","8080"));Serverserver=newServer(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.WebAppContextwebapp=newWebAppContext();webapp.setContextPath("/");webapp.setWar(args[0]);ClassListclasslist=ClassList.setServerDefault(server);// Enable Annotation Scanning.classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration","org.eclipse.jetty.annotations.AnnotationConfiguration");// Set the the WebAppContext as the ContextHandler for the server.server.setHandler(webapp);// Start the server! By using the server.join() the server thread will// join with the current thread. See// "http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join()"// for more details.server.start();server.join();}}
runtime:javaenv:flexruntime_config:operating_system:ubuntu18runtime_version:11entrypoint:'java-cp"*"com.example.appengine.jetty.Mainhelloworld-1.war'handlers:-url:/.*script:this field is required, but ignoredmanual_scaling:instances:1
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-03-07。"],[[["This document guides you through repackaging a Java 8 application as a JAR file compatible with Java 11/17 runtimes, requiring a `Main` class that starts a web server on port 8080."],["The migration process utilizes the `appengine-simple-jetty-main` artifact, which includes a `Main` class with a basic Jetty web server capable of loading a WAR file and packaging the app into an executable JAR."],["To implement the migration, you must add the `appengine-simple-jetty-main` dependency and the `maven-dependency` plugin to your project's `pom.xml` file."],["An `entrypoint` element must be defined in the `app.yaml` file, invoking the `appengine-simple-jetty-main` object and passing the WAR file as an argument."],["The application can be deployed using Maven, using the `mvn package appengine:deploy` command and specifying the project ID if it's not already specified in the `pom.xml` file."]]],[]]