Setting the HTTP Port for Java Applications
Last updated April 09, 2020
Heroku expects a web application to bind its HTTP server to the port defined by the $PORT
environment variable. Many frameworks default to port 8080, but can be configured to use an environment variable instead. In most cases, the port can be configured by adding a parameter to the java
command in an application’s Procfile
, but some frameworks provide a configuration file. The most common Java frameworks are listed here.
Spring Boot
Spring-Boot provides a few different mechanisms for setting the HTTP port. It can be passed an option to the executable JAR file (i.e. the options after the -jar app.jar
options in your java
command):
--server.port=$PORT
Or as a Java system property:
-Dserver.port=$PORT
In both cases, these options can be added to the java
command in an app’s Procfile
. Another option is a configuration element in the application.yml
:
server:
port: $PORT
For more info see Spring Boot documentation on Properties and Configuration
Webapp Runner (Tomcat)
Webapp Runner allows you to launch an exploded or compressed war on your filesystem into a Tomcat container with a simple java -jar
command. It accepts the following option to the executable JAR file (i.e. the options after the -jar webapp-runner.jar
options in your java
command):
--port $PORT
When using the heroku war:deploy
and Heroku Maven Plugin tools, this option is configured for you. For more information, see the Webapp Runner documentation.
Play Framework
The Play Framework, which uses Netty as its server, accepts a Java system property (i.e. an option to the java
command):
-Dhttp.port=$PORT
For more info see the Play documentation for Heroku.
Dropwizard
The Dropwizard framework accepts a Java system property:
-Ddw.server.applicationConnectors[0].port=$PORT
For more information see the Dropwizard Configuration reference.
Thorntail
The Thorntail framework accepts a system property:
-Dswarm.http.port=$PORT
For more information see the WildFly Swarm documentation.
Ratpack
The Ratpack framework, which uses the Netty server, automatically detects the $PORT
environment variable and configures the app to use it.
Grails
The Grails 3 framework produces a WAR file that can be run with either Tomcat or Jetty. See the Webapp Runner section for more information.
For more information see the WildFly Swarm