/
StartServer.java
57 lines (49 loc) · 1.94 KB
/
StartServer.java
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
57
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.jar.JarFile;
import java.util.jar.Manifest;
public class StartServer{
public static void main(String[] args){
try {
System.out.println("Running folder: " + new File(".").getAbsolutePath());
File server = new File("server.jar");
Manifest m = new JarFile(server).getManifest();
ClassLoader loader = new SpongeClassLoader(
new URL[]{server.toURI().toURL()}, StartServer.class.getClassLoader()
);
try {
loader.loadClass(m.getMainAttributes().getValue("Main-Class"))
.getMethod("main", String[].class)
.invoke(null, (Object) args);
} catch (IllegalAccessException | InvocationTargetException e) {
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static class SpongeClassLoader extends URLClassLoader {
public SpongeClassLoader(URL[] urls, ClassLoader parent) {
super(urls, parent);
try {
//making sure the classloaders in forge don't get confused :).
Method ADDURL = URLClassLoader.class.getDeclaredMethod("addURL", URL.class);
ADDURL.setAccessible(true);
for (URL url : urls) {
try {
ADDURL.invoke(getClass().getClassLoader(), url);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
}
}
}