From 0e3da4b5afe18a3cd92d8db7cd95558bb9186d67 Mon Sep 17 00:00:00 2001 From: kyrptonaught Date: Tue, 7 May 2024 01:20:57 -0400 Subject: [PATCH] Switch updater to use a classloader --- .../ToolBox/UpdateBootstrapper.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java b/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java index 4204e13..0923e4b 100644 --- a/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java +++ b/src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java @@ -5,8 +5,10 @@ import net.kyrptonaught.ToolBox.IO.FileHelper; import java.io.BufferedReader; -import java.io.File; import java.io.InputStreamReader; +import java.lang.reflect.Method; +import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; @@ -68,18 +70,12 @@ public static void runToolbox() { } private static void launchJar(List args) { - String[] launchCommands = {"java", "-jar", ".toolbox/launch.jar"}; - - args.addAll(0, List.of(launchCommands)); - try { - new ProcessBuilder(args) - .directory(new File(System.getProperty("user.dir"))) - .inheritIO() - .start() - .waitFor(); - - System.exit(0); + URLClassLoader child = new URLClassLoader(new URL[]{Paths.get(".toolbox/launch.jar").toUri().toURL()}, Main.class.getClassLoader()); + Class classToLoad = Class.forName(Main.class.getName(), true, child); + Method method = classToLoad.getDeclaredMethod("main", String[].class); + Object instance = classToLoad.newInstance(); + method.invoke(instance, (Object) args.toArray(String[]::new)); } catch (Exception e) { e.printStackTrace(); }