Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,11 @@ public static void main(String[] args) throws Exception {
// Note: If you set this to port 0, a randomly available port will be
// assigned. You can find the assigned port in the logs or programmatically
// obtain it.
Server server = new Server(8080);
Server server = new Server(
System.getenv().containsKey("PORT")
? Integer.parseInt(System.getenv().get("PORT"))
: 8080
);

// The WebAppContext is the interface to provide configuration for a web
// application. In this example, the context path is being set to "/" so
Expand Down
6 changes: 5 additions & 1 deletion appengine-java11/custom-entrypoint/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ public class Main {

public static void main(String[] args) throws IOException {
// Create an instance of HttpServer bound to port 8080.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
HttpServer server = HttpServer.create(new InetSocketAddress(
System.getenv().containsKey("PORT")
? Integer.parseInt(System.getenv().get("PORT"))
: 8080
), 0);

// Set root URI path.
server.createContext("/", (var t) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ public class Main {

public static void main(String[] args) throws IOException {
// Create an instance of HttpServer with port 8080.
HttpServer server = HttpServer.create(new InetSocketAddress(8080), 0);
HttpServer server = HttpServer.create(new InetSocketAddress(
System.getenv().containsKey("PORT")
? Integer.parseInt(System.getenv().get("PORT"))
: 8080
), 0);

// Set root URI path.
server.createContext("/", (var t) -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,11 @@ public class Main {

public static void main(String[] args) {
// Starts the webapp on localhost:8080.
Spark.port(8080);
Spark.port(
System.getenv().containsKey("PORT")
? Integer.parseInt(System.getenv().get("PORT"))
: 8080
);
Spark.get("/", (req, res) -> "Hello World!");
}
}