From cd88effb595cc86790628741bbd836b86c6236cc Mon Sep 17 00:00:00 2001 From: Henry Le Grys Date: Sun, 5 Dec 2021 23:43:10 +0000 Subject: [PATCH] Add a check for Java runtime version before setting permgen flag Some newer GraalVM versions appear to be throwing a hard error when the PermGen flag is specified; this commit changes the behaviour so the flag is only passed when the Java version is unknown or older than 1.8 --- .../launcher/launch/JavaProcessBuilder.java | 33 ++++++++++--------- .../com/skcraft/launcher/launch/Runner.java | 9 ++--- 2 files changed, 19 insertions(+), 23 deletions(-) diff --git a/launcher/src/main/java/com/skcraft/launcher/launch/JavaProcessBuilder.java b/launcher/src/main/java/com/skcraft/launcher/launch/JavaProcessBuilder.java index dbdaba0..c28ab35 100644 --- a/launcher/src/main/java/com/skcraft/launcher/launch/JavaProcessBuilder.java +++ b/launcher/src/main/java/com/skcraft/launcher/launch/JavaProcessBuilder.java @@ -27,7 +27,7 @@ public class JavaProcessBuilder { private static final Pattern argsPattern = Pattern.compile("(?:([^\"]\\S*)|\"(.+?)\")\\s*"); - @Getter @Setter private File jvmPath = JavaRuntimeFinder.findBestJavaPath(); + @Getter @Setter private JavaRuntime runtime; @Getter @Setter private int minMemory; @Getter @Setter private int maxMemory; @Getter @Setter private int permGen; @@ -37,7 +37,9 @@ public class JavaProcessBuilder { @Getter private final List args = new ArrayList(); @Getter @Setter private String mainClass; - public void tryJvmPath(File path) throws IOException { + private File getJavaBinPath() throws IOException { + File path = runtime.getDir().getAbsoluteFile(); + // Try the parent directory if (!path.exists()) { throw new IOException( @@ -51,7 +53,7 @@ public class JavaProcessBuilder { path = binDir; } - setJvmPath(path); + return path; } public JavaProcessBuilder classPath(File file) { @@ -81,36 +83,35 @@ public class JavaProcessBuilder { return builder.toString(); } - public List buildCommand() { + public List buildCommand() throws IOException { List command = new ArrayList(); - if (getJvmPath() != null) { - command.add(getJvmPath() + File.separator + "java"); + if (getRuntime() != null) { + File javaBinary = new File(getJavaBinPath(), "java"); + command.add(javaBinary.getAbsolutePath()); } else { command.add("java"); } - for (String flag : flags) { - command.add(flag); - } + command.addAll(flags); if (minMemory > 0) { - command.add("-Xms" + String.valueOf(minMemory) + "M"); + command.add("-Xms" + minMemory + "M"); } if (maxMemory > 0) { - command.add("-Xmx" + String.valueOf(maxMemory) + "M"); + command.add("-Xmx" + maxMemory + "M"); } if (permGen > 0) { - command.add("-XX:MaxPermSize=" + String.valueOf(permGen) + "M"); + // If we know the Java version, only add permsize for 7 or older + if (getRuntime() == null || getRuntime().getMajorVersion() < 8) { + command.add("-XX:MaxPermSize=" + permGen + "M"); + } } command.add(mainClass); - - for (String arg : args) { - command.add(arg); - } + command.addAll(args); return command; } diff --git a/launcher/src/main/java/com/skcraft/launcher/launch/Runner.java b/launcher/src/main/java/com/skcraft/launcher/launch/Runner.java index 96602f5..588a365 100644 --- a/launcher/src/main/java/com/skcraft/launcher/launch/Runner.java +++ b/launcher/src/main/java/com/skcraft/launcher/launch/Runner.java @@ -255,13 +255,8 @@ public class Runner implements Callable, ProgressObservable { .orElse(config.getJavaRuntime()) ); - // Builder defaults to a found runtime or just the PATH `java` otherwise - if (selectedRuntime != null) { - String rawJvmPath = selectedRuntime.getDir().getAbsolutePath(); - if (!Strings.isNullOrEmpty(rawJvmPath)) { - builder.tryJvmPath(new File(rawJvmPath)); - } - } + // Builder defaults to the PATH `java` if the runtime is null + builder.setRuntime(selectedRuntime); List flags = builder.getFlags(); String[] rawJvmArgsList = new String[] {