aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/moe/nea/modernjava/launch/util/ClassLoaderManipulations.java
blob: 09f23680257f5941f44e4a56e6145d69839ca95a (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
package moe.nea.modernjava.launch.util;

import net.minecraft.launchwrapper.Launch;

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

public class ClassLoaderManipulations {

    public static void addToParentClassLoader(File file) {
        try {
            addToParentClassLoader(file.toURI().toURL());
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void addToParentClassLoader(URL file) {
        try {
            Launch.classLoader.addURL(file);
            ClassLoader parentClassLoader = Launch.classLoader.getClass().getClassLoader();
            Method addUrl = parentClassLoader.getClass().getDeclaredMethod("addURL", URL.class);
            addUrl.setAccessible(true);
            addUrl.invoke(parentClassLoader, file);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}