By default, Java apps deployed on Heroku will be running under JDK 1.8, as you can see when deploying an app via Heroku CLI:
To change this default Java version, you have to create the system.properties file under the project’s root directory, with the following content:
java.runtime.version=<jdk_version>
For example, to use JDK 15:
java.runtime.version=15
As of October 2021, Heroku supports Java versions 1.7, 1.8, 11, 13, 15, 16 and 17.
Then, if you deploy via Git push using Heroku CLI, you need to run the following commands to redeploy the app:
git add .
git commit –m “use java version 15”
git push heroku master
Then you should see it is installing JDK 15 for compiling and building the app:
Now your app is running under the JDK version you specified.
To see the steps in action, watch this video: