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.
- Read the number in the error: 52 means the file needs Java 8, 61 means Java 17, 65 means Java 21.
- Install a JDK of that version.
- For building, set JAVA_HOME to it, or choose it as the Gradle JVM in your IDE.
- For playing, set the Java executable in your launcher’s profile.
- Add the log line from this page and check logs/latest.log for the Java it really used.
For each loader and version
Section titled “For each loader and version”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.versionSystem.getPropertyLogUtils.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.versionSystem.getPropertyLogUtils.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();Fabric 1.18 to 1.18.1
Section titled “Fabric 1.18 to 1.18.1”Uses:
Runtime.versionSystem.getPropertyLogUtils.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();Fabric 1.16.5 to 1.17.1
Section titled “Fabric 1.16.5 to 1.17.1”Uses:
Runtime.versionSystem.getPropertyLogUtils.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();Forge 1.16.5 to 1.17.1
Section titled “Forge 1.16.5 to 1.17.1”Uses:
System.getPropertyLogManager.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();Forge 1.12.2
Section titled “Forge 1.12.2”Uses:
System.getPropertyLogManager.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();Sources
Section titled “Sources”- https://docs.fabricmc.net/1.21.1/develop/events
- https://docs.neoforged.net/docs/1.21.1/gettingstarted/modfiles
Related how-tos
Section titled “Related how-tos”- Update my mod to a new Minecraft version
- Armor that gives a potion effect
- Do something when a player joins
- Find out why a mod is not in the mod list
- Fix ClassNotFound and NoSuchMethod errors
In Linen
Section titled “In Linen”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.