# Fix Unsupported Major Minor Version Error in Java

> Resolve the UnsupportedClassVersionError by aligning your JRE and JDK versions, recompiling with --release for older targets, or updating JAVA_HOME and PATH

**URL:** https://sentry.io/answers/how-to-fix-unsupported-major-minor-version-error-in-java/

---

## The Problem

You get the following error when trying to run your Java program:

```
java.lang.UnsupportedClassVersionError: Unsupported major.minor version
```

## The Solution

A `major.minor` version error occurs when the Java Runtime Environment (JRE) version used to run a program is older than the Java Development Kit (JDK) version used to compile its code. You can resolve this by:

- Checking and updating your JDK and JRE versions
- Compiling the code with an older version of JDK
- Ensuring you’re using the correct JAVA_HOME and PATH
- Configuring your IDE for a specific Java version

### Check the Java Version Used to Compile Code

If you have access to the compiled `.class` or `.jar` files, use the following command to check the version they were compiled with:

```
javap -verbose MyClass | grep "major"
```

You will see an output like:

```
major version: 61
```

### Use the Correct JDK and JRE Versions

Ensure the JRE running your application is at least as recent as the JDK used to compile the code. For example, if the class is compiled with Java 17 (major version 61), you need to run it with Java 17 or higher.

```
java -version
javac -version
```

If the `java` version is older than `javac`, install the appropriate JDK or JRE version, or update your Java version.

### Compile with a Lower Java Version (Optional)

If you need to run the code on a system with an older JRE, you can recompile your Java code with compatibility flags to generate bytecode compatible with older versions.

The following command compiles the code to be compatible with Java 8 (major version 52):

```bash
javac --release 8 MyClass.java
```

### Set the Correct `JAVA_HOME` and `PATH`

Ensure that your environment is configured to use the correct version of Java by setting `JAVA_HOME` and `PATH`.

On Linux/macOS:

```bash
export JAVA_HOME=/path/to/your/java
export PATH=$JAVA_HOME/bin:$PATH
```

On Windows (CMD):

```bash
set JAVA_HOME=C:\path\to\your\java
set PATH=%JAVA_HOME%\bin;%PATH%
```

### Install the Correct Java Version

If the code requires a specific version, download and install it from the [official Java website](https://www.oracle.com/java/technologies/downloads/?er=221886) or an open-source distribution like [Eclipse Adoptium](https://adoptium.net/).

### Update the Java Version in Your IDE

If you are using an IDE like IntelliJ IDEA or Eclipse, make sure the project settings are configured to use the correct JDK version.

## Note

Each version of Java assigns a unique `major.minor` version number to compiled class files, so running any class with a newer major version on an older Java virtual machine will return `major.minor` version error.

## Further Reading

- The [Java `UnsupportedClassVersionError`](https://docs.oracle.com/en/java/javase/22/docs/api/java.base/java/lang/UnsupportedClassVersionError.html) documentation

---

*Source: [sentry.io/answers/how-to-fix-unsupported-major-minor-version-error-in-java/](https://sentry.io/answers/how-to-fix-unsupported-major-minor-version-error-in-java/)*
