Skip to content

Fix a wrong Java version error in a Minecraft mod

Each Minecraft era needs a minimum Java: 8 for 1.16.5 and older, 16 for 1.17, 17 for 1.18 to 1.20.4, and 21 for 1.20.5 to 1.21.11; newer versions can ask for a later Java still. ‘Unsupported class file major version’ (or ‘compiled by a more recent version’) means the Java running the program is older than the one that built it: major version 52 is Java 8, 61 is Java 17 and 65 is Java 21. Fix the Java in one of three places: the launcher’s Java path for playing, JAVA_HOME or the Gradle JVM for building, and the IDE’s project SDK.

Works on Minecraft 1.20.1 to 26.3 on NeoForge; Minecraft 1.16.5 to 26.3 on Fabric; Minecraft 1.16.5 to 1.20.1 on Forge; Minecraft 1.12.2 on Forge. Each example was compiled against its loader and version before it shipped in Linen.

  1. Read the number in the error: 52 means the file needs Java 8, 61 means Java 17, 65 means Java 21.
  2. Install a JDK of that version.
  3. For building, set JAVA_HOME to it, or choose it as the Gradle JVM in your IDE.
  4. For playing, set the Java executable in your launcher’s profile.
  5. Add the log line from this page and check logs/latest.log for the Java it really used.

The example mod is examplemod in the package com.example.examplemod. Use your own mod id and package.

Fabric 1.18.2 to 1.21.1, Fabric 1.21.2 to 26.3

Section titled “Fabric 1.18.2 to 1.21.1, Fabric 1.21.2 to 26.3”

Uses:

  • Runtime.version
  • System.getProperty
  • LogUtils.getLogger

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import com.mojang.logging.LogUtils;
import org.slf4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogUtils.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", Runtime.version().feature(), System.getProperty("java.home"));
}
}

In your main mod class, inside onInitialize:

JavaInfoJavaInfo.log();

NeoForge 1.20.1 to 1.21.1, NeoForge 1.21.2 to 26.3, Forge 1.18.2 to 1.20.1

Section titled “NeoForge 1.20.1 to 1.21.1, NeoForge 1.21.2 to 26.3, Forge 1.18.2 to 1.20.1”

Uses:

  • Runtime.version
  • System.getProperty
  • LogUtils.getLogger

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import com.mojang.logging.LogUtils;
import org.slf4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogUtils.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", Runtime.version().feature(), System.getProperty("java.home"));
}
}

In your main mod class, inside <init>:

JavaInfoJavaInfo.log();

Uses:

  • Runtime.version
  • System.getProperty
  • LogUtils.getLogger

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogManager.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", Runtime.version().feature(), System.getProperty("java.home"));
}
}

In your main mod class, inside onInitialize:

JavaInfoJavaInfo.log();

Uses:

  • Runtime.version
  • System.getProperty
  • LogUtils.getLogger

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogManager.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", System.getProperty("java.version"), System.getProperty("java.home"));
}
}

In your main mod class, inside onInitialize:

JavaInfoJavaInfo.log();

Uses:

  • System.getProperty
  • LogManager.getLogger

Forge 1.16.5 runs on Java 8: java.version reads like 1.8.0_412.

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogManager.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", System.getProperty("java.version"), System.getProperty("java.home"));
}
}

In your main mod class, inside <init>:

JavaInfoJavaInfo.log();

Uses:

  • System.getProperty
  • LogManager.getLogger

Forge 1.12.2 runs on Java 8: java.version reads like 1.8.0_412.

src/main/java/com/example/examplemod/JavaInfoJavaInfo.java

package com.example.examplemod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
// Java Info: writes the Java the game runs on to logs/latest.log. The launcher can start Minecraft with a different Java than the one in your terminal.
public final class JavaInfoJavaInfo {
private static final Logger LOGGER = LogManager.getLogger();
private JavaInfoJavaInfo() {
}
public static void log() {
LOGGER.info("Running on Java {} from {}", System.getProperty("java.version"), System.getProperty("java.home"));
}
}

In your main mod class, inside <init>:

JavaInfoJavaInfo.log();

Linen’s How-tos library has this page too, with the example for your own mod’s loader and Minecraft version. Hover a name in your code and Linen points to the how-tos that explain it. Both are free. Download Linen.