Skip to content

Instantly share code, notes, and snippets.

@fcamel
Created June 10, 2011 11:40
Show Gist options
  • Save fcamel/1018669 to your computer and use it in GitHub Desktop.
Save fcamel/1018669 to your computer and use it in GitHub Desktop.
Use reflection to call main method
static boolean callMain(String className, String[] mainArgs) {
try {
Class cls = Class.forName(className);
Class[] parameterTypes = new Class[1];
parameterTypes[0] = String[].class;
Method main = cls.getMethod("main", parameterTypes);
Object[] args = new Object[] { mainArgs };
main.invoke(null, args);
} catch (Throwable e) {
System.err.println(e);
return false;
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment