Skip to content

Commit

Permalink
Read Minecraft version and required Java runtime from jar manifest
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jun 18, 2024
1 parent f07f626 commit 81aa51b
Showing 1 changed file with 44 additions and 3 deletions.
47 changes: 44 additions & 3 deletions java6/src/main/java/io/papermc/paperclip/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,36 @@

package io.papermc.paperclip;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.jar.Manifest;

public final class Main {

public static void main(final String[] args) {
if (getJavaVersion() < 17) {
System.err.println("Minecraft 1.19 requires running the server with Java 17 or above. " +
"For information on how to update Java, see https://docs.papermc.io/misc/java-install");
final Manifest manifest = manifest();
final String javaVerString = manifest.getMainAttributes().getValue("Java-Runtime-Major-Version");
if (javaVerString == null) {
throw new RuntimeException("Manifest missing Java-Runtime-Major-Version");
}
final int javaVer;
try {
javaVer = Integer.parseInt(javaVerString);
} catch (final NumberFormatException e) {
throw new RuntimeException("Failed to parse Java-Runtime-Major-Version", e);
}
final String mcVer = manifest.getMainAttributes().getValue("Minecraft-Version");
if (mcVer == null) {
throw new RuntimeException("Manifest missing Minecraft-Version");
}
if (getJavaVersion() < javaVer) {
System.err.printf(
"Minecraft %s requires running the server with Java %s or above. " +
"For information on how to update Java, see https://docs.papermc.io/misc/java-install\n",
mcVer,
javaVer
);
System.exit(1);
}

Expand All @@ -29,6 +51,25 @@ public static void main(final String[] args) {
}
}

@SuppressWarnings("ThrowFromFinallyBlock")
private static Manifest manifest() {
final InputStream in = Main.class.getClassLoader().getResourceAsStream("META-INF/MANIFEST.MF");
if (in == null) {
throw new RuntimeException("Failed to locate manifest");
}
try {
return new Manifest(in);
} catch (final IOException e) {
throw new RuntimeException("Failed to read manifest", e);
} finally {
try {
in.close();
} catch (final IOException e) {
throw new RuntimeException("Exception closing stream", e);
}
}
}

private static int getJavaVersion() {
final String version = System.getProperty("java.specification.version");
final String[] parts = version.split("\\.");
Expand Down

0 comments on commit 81aa51b

Please sign in to comment.