--- /home/tromey/gnu/Nightly/classpath/classpath/gnu/java/lang/MainThread.java 2004-06-08 02:24:16.000000000 -0600 +++ gnu/java/lang/MainThread.java 2004-07-30 02:19:01.000000000 -0600 @@ -39,7 +39,8 @@ package gnu.java.lang; -import java.lang.reflect.*; +import java.util.jar.Attributes; +import java.util.jar.JarFile; /** * MainThread is a Thread which uses the main() method of some class. @@ -47,35 +48,82 @@ * @author John Keiser * @author Tom Tromey (tromey@redhat.com) */ -public class MainThread +final class MainThread extends Thread { + // If the user links statically then we need to ensure that these + // classes are linked in. Otherwise bootstrapping fails. These + // classes are only referred to via Class.forName(), so we add an + // explicit mention of them here. + static final Class Kcert = java.security.cert.Certificate.class; + static final Class Kfile = gnu.java.net.protocol.file.Handler.class; + static final Class Khttp = gnu.java.net.protocol.http.Handler.class; + static final Class Kjar = gnu.java.net.protocol.jar.Handler.class; + static final Class Klocale = gnu.java.locale.LocaleInformation.class; + static final Class Kcalendar = gnu.java.locale.Calendar.class; + // Private data. - String[] args; - Method mainMethod; + private Class klass; + private String klass_name; + private String[] args; + private boolean is_jar; + + public MainThread(Class k, String[] args) + { + super(null, null, "main"); + klass = k; + this.args = args; + } - public MainThread(String classname, String[] args) - throws ClassNotFoundException, NoSuchMethodException + public MainThread(String classname, String[] args, boolean is_jar) { - Class found = Class.forName(classname); - Class[] argTypes = new Class[1]; - argTypes[0] = args.getClass(); - mainMethod = found.getMethod("main", argTypes); + super (null, null, "main"); + klass_name = classname; this.args = args; + this.is_jar = is_jar; } public void run() { + if (is_jar) + klass_name = getMain(klass_name); + + if (klass == null) + { + try + { + klass = Class.forName(klass_name); + } + catch (ClassNotFoundException x) + { + throw new NoClassDefFoundError(klass_name); + } + } + + call_main(); + } + + private String getMain(String name) + { + String mainName = null; try { - mainMethod.invoke(null,args); + JarFile j = new JarFile(name); + Attributes a = j.getManifest().getMainAttributes(); + mainName = a.getValue(Attributes.Name.MAIN_CLASS); } - catch(IllegalAccessException e) + catch (Exception e) { // Ignore. } - catch(InvocationTargetException e) + + if (mainName == null) { - // Ignore. + System.err.println("Failed to load Main-Class manifest attribute from " + + name); + System.exit(1); } + return mainName; } + + private native void call_main(); }