32

I am trying to run a Spring Boot app in IntelliJ.

But when I try to run I get the following error message:

Exception in thread "main" java.lang.UnsupportedClassVersionError: myProject has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0

Here is the JRE location under my Run Configuration:

enter image description here

I have tried installing a couple of different Java versions here:

enter image description here

Here are my current system / environment variables:

Environment Variables:

JAVA_HOME: C:\Program Files\Java\jdk-11.0.12\bin

System Variables:

Path: C:\Program Files\Java\jdk-11.0.12\bin ....

I tried to find a JRE that matched the version, but haven't been able to find it.

Can someone please tell me what changes I need to make so I can successfully run this application locally?

user9847788
  • 2,135
  • 5
  • 31
  • 79
  • 2
    Did you try changing the JRE location under "Run Configuration"? It says 1.8 now and you need it to be 11+ – that other guy Jan 05 '22 at 22:31
  • You should really uninstall the JRE8 since it is redundant with JDK8. And you should uninstall JDK8 because you don't really need it anymore – OneCricketeer Jan 05 '22 at 22:32
  • Does this answer your question? [Class has been compiled by a more recent version of the Java Environment](https://stackoverflow.com/questions/47457105/class-has-been-compiled-by-a-more-recent-version-of-the-java-environment) – Gurkirat Singh Guliani Jan 05 '22 at 22:32
  • Note: `JAVA_HOME` should not include the bin folder, and your PATH can use `%JAVA_HOME%\bin` – OneCricketeer Jan 05 '22 at 22:33
  • There is a JRE inside the Java 8 folder, but no JRE inside the 11 folder – user9847788 Jan 05 '22 at 22:33
  • 2
    Sure. The distinction between them was removed after Java 8. You still don't need both. [_The JDK includes the JRE, so you do not have to download both separately_](https://docs.oracle.com/javase/9/install/overview-jdk-9-and-jre-9-installation.htm#JSJIG-GUID-8677A77F-231A-40F7-98B9-1FD0B48C346A) – OneCricketeer Jan 05 '22 at 22:34
  • 1
    Regardless, there is other information that is missing. Are you using Maven/Gradle? Is **that** compiling your code to Java11? Then IntelliJ's run config is set to Java8? – OneCricketeer Jan 05 '22 at 22:37
  • So if there's no JRE folder in version 11, what should I specify under JRE in the Run Config? – user9847788 Jan 05 '22 at 22:37
  • 1
    It's using Gradle – user9847788 Jan 05 '22 at 22:38
  • 2
    Okay, and do you see a line that has something like `sourceCompatibility 11`? Or `targetCompatibility 11`? Can you change that to 8 and run it? – OneCricketeer Jan 05 '22 at 22:40
  • I forgot this issue with Maven POM about source and target compatibility. That could be it, but I think in the case of Maven, you will have a different error. I had a similar issue when I migrated from Java 8 to Java 14 about 3 years ago. But honestly, I don't remember what the error was. Of course, regardless of that, it is a great suggestion to check this and make sure the compatibility settings points to the same version as the JDK used to compile. – hfontanez Jan 05 '22 at 22:50
  • 1
    @OneCricketeer I updated `sourceCompatibility` from 11 to 8, but I still get the error. When I update this what am I to specify as the JRE under the Run Configuration? – user9847788 Jan 05 '22 at 22:55
  • I think you need to update IntelliJ. Mine says "JDK or JRE" in Run Config panel. And in "Platform Settings > SDK", it _only_ says "JDK home path". Like I (and documentation) says JDK **includes** the JRE, so they are the same paths. – OneCricketeer Jan 05 '22 at 23:20
  • Also, the fastest solution to your problem would be running `gradle bootRun` instead of messing with OS or IDE variables – OneCricketeer Jan 05 '22 at 23:23

2 Answers2

25

Open IntelliJ on your project and go to File >> Project Structure >> Project and on the option Project SDK Select java 11 and apply the changes.

Intellij Project structure

Edit--------

Then try these options on File >> Settings.

1.Set Gradle JVM Home Gradle JVM

2.Set the JDK version in the Project module settings JDK version in the Project module

3.Check the JDK version in the Modules version in the Modules

Close IntelliJ and open it again.

Juan Berrospe
  • 351
  • 2
  • 4
9

If you are trying to execute the code outside your IDE...

The problem is Windows....

Open your command prompt and type "where java". When you do this, you will notice a "weird" path that looks something like this

C:\Program Files\Common Files\Oracle\Java\javapath\java.exe

I can't recall what Windows version introduced this shortcut, but I want to say it was Windows 7. The problem is that this shortcut points to a Runtime Environment that is more recent (newer) than the Java version that was used to compile the code. In other words, Java is backwards compatible; not forward compatible. Compiled code cannot run in a newer Java version.

How do you fix this?

One way is to go to your SYSTEM Environment Variables and edit the Path system variable and add JAVA_HOME at the very beginning of the paths. It will be something like this

%JAVA_HOME%; {YOUR OTHER PATHS HERE}

If you are trying to execute within the IDE, the problem is similar. POTENTIALLY, you can have a conflict between your PROJECT properties and your global IDE properties, where the Runtime Environment points to a newer version of Java than the JDK used to compile. That said...

I had a very weird issue with an application I worked on years ago. It was a client/server application and a coworker of mine was having this issue because, unbeknownst to her, there was version of the server running in the background that was causing the conflict. So, if you have a similar type of application, check your processes using Task Manager and make sure you either 1) shutdown this application and try again, or 2) Uninstall that application and reinstall the new one. In her case, she tried rebooting her PC but that didn't work because the process would start automatically after each reboot.

hfontanez
  • 5,774
  • 2
  • 25
  • 37
  • AFAIK, IntelliJ always refers to the full java exe path defined in its own settings, and doesn't depend on OS environment – OneCricketeer Jan 05 '22 at 22:39
  • @OneCricketeer I don't know much about IntelliJ per se, but I helped a coworker fixed a similar problem about 5 years ago. I edited my answer with this strange edge case, just in case. Our issue baffled us for almost an entire day (believe it or not) – hfontanez Jan 05 '22 at 22:46