diff --git a/java6/src/main/java/io/papermc/paperclip/Main.java b/java6/src/main/java/io/papermc/paperclip/Main.java index 5c30d9a..b5aed0e 100644 --- a/java6/src/main/java/io/papermc/paperclip/Main.java +++ b/java6/src/main/java/io/papermc/paperclip/Main.java @@ -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); } @@ -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("\\.");