MainThread.java 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* gnu.java.lang.MainThread
  2. Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
  3. 2006, 2008 Free Software Foundation, Inc.
  4. This file is part of GNU Classpath.
  5. GNU Classpath is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2, or (at your option)
  8. any later version.
  9. GNU Classpath is distributed in the hope that it will be useful, but
  10. WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with GNU Classpath; see the file COPYING. If not, write to the
  15. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301 USA.
  17. Linking this library statically or dynamically with other modules is
  18. making a combined work based on this library. Thus, the terms and
  19. conditions of the GNU General Public License cover the whole
  20. combination.
  21. As a special exception, the copyright holders of this library give you
  22. permission to link this library with independent modules to produce an
  23. executable, regardless of the license terms of these independent
  24. modules, and to copy and distribute the resulting executable under
  25. terms of your choice, provided that you also meet, for each linked
  26. independent module, the terms and conditions of the license of that
  27. module. An independent module is a module which is not derived from
  28. or based on this library. If you modify this library, you may extend
  29. this exception to your version of the library, but you are not
  30. obligated to do so. If you do not wish to do so, delete this
  31. exception statement from your version. */
  32. package gnu.java.lang;
  33. import java.io.File;
  34. import java.util.jar.Attributes;
  35. import java.util.jar.JarFile;
  36. /**
  37. * MainThread is a Thread which uses the main() method of some class.
  38. *
  39. * @author John Keiser
  40. * @author Tom Tromey (tromey@redhat.com)
  41. */
  42. final class MainThread extends Thread
  43. {
  44. // If the user links statically then we need to ensure that these
  45. // classes are linked in. Otherwise bootstrapping fails. These
  46. // classes are only referred to via Class.forName(), so we add an
  47. // explicit mention of them here.
  48. static final Class Kcert = java.security.cert.Certificate.class;
  49. static final Class Kfile = gnu.java.net.protocol.file.Handler.class;
  50. static final Class Khttp = gnu.java.net.protocol.http.Handler.class;
  51. static final Class Kjar = gnu.java.net.protocol.jar.Handler.class;
  52. // Private data.
  53. private Class klass;
  54. private String klass_name;
  55. private String[] args;
  56. private boolean is_jar;
  57. public MainThread(Class k, String[] args)
  58. {
  59. super(null, null, "main");
  60. klass = k;
  61. this.args = args;
  62. }
  63. public MainThread(String classname, String[] args, boolean is_jar)
  64. {
  65. super (null, null, "main");
  66. klass_name = classname;
  67. this.args = args;
  68. this.is_jar = is_jar;
  69. }
  70. public void run()
  71. {
  72. if (is_jar)
  73. klass_name = getMain(klass_name);
  74. if (klass == null)
  75. {
  76. try
  77. {
  78. ClassLoader cl = ClassLoader.getSystemClassLoader();
  79. // Permit main class name to be specified in file-system format.
  80. klass_name = klass_name.replace(File.separatorChar, '.');
  81. klass = cl.loadClass(klass_name);
  82. }
  83. catch (ClassNotFoundException x)
  84. {
  85. NoClassDefFoundError ncdfe = new NoClassDefFoundError(klass_name);
  86. ncdfe.initCause(x);
  87. throw ncdfe;
  88. }
  89. }
  90. call_main();
  91. }
  92. private String getMain(String name)
  93. {
  94. String mainName = null;
  95. try
  96. {
  97. JarFile j = new JarFile(name);
  98. Attributes a = j.getManifest().getMainAttributes();
  99. mainName = a.getValue(Attributes.Name.MAIN_CLASS);
  100. }
  101. catch (Exception e)
  102. {
  103. // Ignore.
  104. }
  105. if (mainName == null)
  106. {
  107. System.err.println("Failed to load Main-Class manifest attribute from "
  108. + name);
  109. System.exit(1);
  110. }
  111. return mainName;
  112. }
  113. // Note: this function name is known to the stack tracing code.
  114. // You shouldn't change this without also updating stacktrace.cc.
  115. private native void call_main();
  116. }