Skip to content

Commit

Permalink
Switch updater to use a classloader
Browse files Browse the repository at this point in the history
  • Loading branch information
kyrptonaught committed May 7, 2024
1 parent e17cb7d commit 0e3da4b
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions src/main/java/net/kyrptonaught/ToolBox/UpdateBootstrapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -68,18 +70,12 @@ public static void runToolbox() {
}

private static void launchJar(List<String> 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();
}
Expand Down

0 comments on commit 0e3da4b

Please sign in to comment.