aboutsummaryrefslogtreecommitdiff
path: root/target/src/main/java/moe/nea/modernjava/target/RelaunchEntryPoint.java
blob: f30de3f0f9fd2ed16e799ca33ae66b234c00d7a4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package moe.nea.modernjava.target;

import java.io.File;
import java.lang.reflect.Method;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Arrays;

public class RelaunchEntryPoint {
    public static void log(String text) {
        System.out.println("[MODERNJAVA] " + text);

    }

    public static void main(String[] args) {
        log("This is modern java relaunch talking!");
        try {
            setupLaunch(args);
        } catch (Throwable t) {
            log("Encountered exception during relaunching");
            t.printStackTrace();
        }
    }

    private static void setupLaunch(String[] args) throws Exception {
        log("Original SQLException: " + Thread.currentThread().getContextClassLoader().loadClass("java.sql.SQLException"));
        log("Setting up fake launch class loader");
        ClassLoader urlClassLoader = new URLClassLoader(retrieveClassPath(), Thread.currentThread().getContextClassLoader());
        log("Created fake url class loader");
        Thread.currentThread().setContextClassLoader(urlClassLoader);
        log("Handing off to Launch");
        Class<?> launchClass = urlClassLoader.loadClass("net.minecraft.launchwrapper.Launch");
        log("LaunchClass: " + launchClass);
        log("LaunchClass ClassLoader: " + launchClass.getClassLoader());

        log("LaunchClass ClassLoader SQLException: " + urlClassLoader.loadClass("java.sql.SQLException"));


        Method main = launchClass.getMethod("main", String[].class);
        main.invoke(null, new Object[]{args});
    }

    private static URL[] retrieveClassPath() throws MalformedURLException {
        String property = System.getProperty("modernjava.relaunchclasspath");
        String[] split = property.split(File.pathSeparator);
        URL[] urls = new URL[split.length];
        for (int i = 0; i < split.length; i++) {
            urls[i] = new File(split[i]).toURI().toURL();
        }
        log("Retrieved classpath: " + Arrays.toString(urls));
        return urls;
    }


}