In this article, I’d like to share with you guys a solution to fix the error “invalid target release” which developers often face in deploying Java applications onto Heroku cloud platform.

You know, when deploying a Maven project for a Java application onto Heroku via Git push in command line, you may get the following error:

remote: [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project [ProjectName]: Fatal error compiling: invalid target release: 11 -> [Help 1]

This error means that your project specifies Java version is 11 but the new dyno (virtualized Unix container) being created with older JDK version, e.g. OpenJDK 1.8 by default - as you can see in the logs:

remote: -----> Building on the Heroku-22 stack

remote: -----> Determining which buildpack to use for this app

remote: -----> Java app detected

remote: -----> Installing OpenJDK 1.8... done

remote: -----> Executing Maven

Whereas in the pom.xml file:

<project ..>

	<properties>
		<java.version>11</java.version>
	</properties>
	
</project>
Understanding this, there are 2 solutions to fix this “invalid target release”:

- Change Java version of your Maven project to the one installed by Heroku

- Change JDK version installed by Heroku for new apps

Let’s dive into each solution in details.



 

1. Fix “invalid target release” error by changing project’s Java version

Use this solution if you’re sure that your application still works normally with the default Java version used by Heroku (OpenJDK 1.8 by default). So just update Java version in the pom.xml file as follows:

<project ..>

	<properties>
		<java.version>1.8</java.version>
	</properties>
	
</project>
Commit this change using the following commands:

      git add pom.xml

git commit -m “change Java version to 1.8”

Then use git push heroku master command to deploy the app again. The “invalid target release” error will be gone and the app will be deployed successfully.

NOTES: to know which JDK version installed by Heroku, see the beginning lines of the deploy logs as shown above. And check this article to learn more about changing Java version for Maven project.

 

2. Fix “invalid target release” error by changing JDK version on Heroku

Use this solution if your app must run with the Java version specified in the pom.xml file. So you need to tell Heroku to install a matching JDK version. Create a text file named system.properties under project root directory, with the following content:

java.runtime.version=11

Then run the following Git commands to commit the change:

git add system.properties

git commit -m “change Heroku java version to 11”

And run git push heroku master command to deploy the app again. You’ll get rid of the “invalid target release” error and get your app deployed.

That’s my solution to fix Heroku Java deploy Maven error invalid target release. I hope you find it helpful. I recommend you watch the following video to see how to fix this error in action:

 

Other Heroku tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.