Skip to content

Commit 6a0352c

Browse files
committed
Try a little harder to find the main method
1 parent 5fb282b commit 6a0352c

1 file changed

Lines changed: 30 additions & 7 deletions

File tree

  • src/main/java/net/minecraftforge/launcher

src/main/java/net/minecraftforge/launcher/Main.java

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.invoke.MethodHandle;
2121
import java.lang.invoke.MethodHandles;
2222
import java.lang.invoke.MethodType;
23+
import java.lang.reflect.Method;
2324
import java.util.zip.ZipEntry;
2425
import java.util.zip.ZipFile;
2526

@@ -121,13 +122,8 @@ private static Launcher run(String[] args) throws Throwable {
121122
LOGGER.pop(indent);
122123

123124
LOGGER.info("Looking for main class: " + mainClass);
124-
MethodHandle mainMethod;
125-
try {
126-
Class<?> main = Class.forName(mainClass);
127-
mainMethod = MethodHandles.publicLookup().findStatic(main, "main", MethodType.methodType(void.class, String[].class));
128-
} catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException e) {
129-
throw new IllegalStateException("Could not find main class!", e);
130-
}
125+
Class<?> main = findMainClass(mainClass);
126+
MethodHandle mainMethod = findMainMethod(main);
131127

132128
LOGGER.info("Sanitizing Minecraft arguments");
133129
for (int i = 0; i < split.mc.length; i++) {
@@ -139,6 +135,33 @@ private static Launcher run(String[] args) throws Throwable {
139135
return new Launcher(mainClass, mainMethod, split.mc);
140136
}
141137

138+
private static Class<?> findMainClass(String mainClass) {
139+
try {
140+
return Class.forName(mainClass);
141+
} catch (ClassNotFoundException e) {
142+
throw new IllegalStateException("Could not find main class!", e);
143+
}
144+
}
145+
146+
private static MethodHandle findMainMethod(Class<?> main) {
147+
MethodHandles.Lookup lookup = MethodHandles.lookup();
148+
try {
149+
return lookup.findStatic(main, "main", MethodType.methodType(void.class, String[].class));
150+
} catch (IllegalAccessException e) {
151+
// Old versions of the bootstrap lib didn't export the class, so lets try with some basic reflection
152+
try {
153+
Method mtd = main.getDeclaredMethod("main", String[].class);
154+
return lookup.unreflect(mtd);
155+
} catch (NoSuchMethodException | IllegalAccessException ex) {
156+
IllegalStateException error = new IllegalStateException("Could not find main(String[]) in " + main.getName() + '!', e);
157+
error.addSuppressed(e);
158+
throw error;
159+
}
160+
} catch (NoSuchMethodException e) {
161+
throw new IllegalStateException("Could not find main(String[]) in " + main.getName() + '!', e);
162+
}
163+
}
164+
142165
private static final class Launcher {
143166
private final String name;
144167
private final MethodHandle main;

0 commit comments

Comments
 (0)