如果您要遷移至最新的支援 Java 版本,且應用程式不會使用舊版套裝服務,則必須將 App Engine Java 8 網路應用程式重新封裝為可執行的 JAR 檔案。
您的應用程式必須有 Main 類別,啟動網路伺服器,以便回應通訊埠 8080 的 HTTP 要求,該通訊埠可能由 PORT 環境變數指定。
例如:
importcom.sun.net.httpserver.HttpServer;importjava.io.IOException;importjava.io.OutputStream;importjava.net.InetSocketAddress;publicclassMain{publicstaticvoidmain(String[]args)throwsIOException{// Create an instance of HttpServer bound to port defined by the // PORT environment variable when present, otherwise on 8080.intport=Integer.parseInt(System.getenv().getOrDefault("PORT","8080"));HttpServerserver=HttpServer.create(newInetSocketAddress(port),0);// Set root URI path.server.createContext("/",(vart)->{byte[]response="Hello World from Google App Engine Java 11.".getBytes();t.sendResponseHeaders(200,response.length);try(OutputStreamos=t.getResponseBody()){os.write(response);}});// Start the server.server.start();}}
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-08 (世界標準時間)。"],[[["Migrating to the latest supported Java version requires repackaging App Engine Java 8 web applications into executable JAR files if they don't utilize legacy bundled services."],["The application's `Main` class must initialize a web server to handle HTTP requests on port 8080, potentially configured via the `PORT` environment variable."],["The `appengine-simple-jetty-main` artifact offers a `Main` class with a basic Jetty server to load WAR files, facilitating the creation of an executable JAR."],["To correctly install the dependencies, you must add the `maven-dependency` plugin in your project's `pom.xml` file and make sure that the files are installed to the `${build.directory}/appengine-staging` directory."],["The `app.yaml` file needs an `entrypoint` that references the `appengine-simple-jetty-main` object and provides your WAR file as an argument, as well as to correctly deploy, `gcloud app deploy` or `mvn package appengine:deploy -Dapp.deploy.projectId=PROJECT_ID` can be used."]]],[]]