Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4827318

System property for main class

    XMLWordPrintable

    Details

    • Type: Enhancement
    • Status: Open
    • Priority: P4
    • Resolution: Unresolved
    • Affects Version/s: 1.3.1, 1.4.1
    • Fix Version/s: None
    • Component/s: tools
    • Labels:
    • Subcomponent:
    • CPU:
      x86
    • OS:
      linux, windows_2000

      Description

      Name: nt126004 Date: 03/04/2003


      FULL PRODUCT VERSION :
      java version "1.4.1_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_01-b01)
      Java HotSpot(TM) Client VM (build 1.4.1_01-b01, mixed mode)


      FULL OPERATING SYSTEM VERSION :

      glibc-2.2.4-25mdk
      Kernel: Linux 2.4.18-6mdk #1 Fri Mar 15 02:59:08 CET 2002
      i686 unknown
        Release: Mandrake Linux release 8.2 (Bluebird) for i586

      ADDITIONAL OPERATING SYSTEMS :

      Should be OS independent.

      A DESCRIPTION OF THE PROBLEM :
      Because I'm lazy I hate writing and even seeing main methods
      in every single one of my many test classes. However the
      main(String[]) method is the universal way of launching
      code. So I would like to see a system property giving the
      name of the class to be executed. Note, this is not necessarily
      the class in which the method is declared.

      If the property was called java.class.main, then the
      supplied code should test whichever subclass was selected.

      The existing behaviour is that if the target class does not have a main,
      then its superclasses are checked for a main method. That's exactly the
      behaviour we want. Run a class and the superclass main gets invoked.
      That method should then be able to determine which subclass was actually
      requested.


      It occurs to me that it is possible to approximate the new required
      behaviour with target classes requiring only code that does not need to
      restate the class name. The somewhat dirty code below illustrates the
      technique for 1.4. The code uses a stack trace to identify the class
      This also demonstrates the selection of the superclass' main. However,
      this workaround is still unpleasant and requires code in the subclass.

      public class MyTestCase extends TestCaseMain {
           static /*public void main(String[] args)*/ { main(new Error()); }

           public void testObvious() {
               assertTrue(true);
           }
      }
      public abstract class TestCaseMain extends junit.framework.TestCase {
           protected static void main(Throwable traceable) {
              try {
                  junit.textui.TestRunner.run(Class.forName(
                          traceable.getStackTrace()[0].getClassName()
                  ));
                  System.exit(0);
              } catch (Throwable exc) {
                  exc.printStackTrace();
                  System.exit(1);
              }
           }
      }


      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      abstract class TestBase {
          public static void main(String[] args) throws Throwable {
              final String mainClassName;
              try {
                  mainClassName = System.getProperty("java.class.main");
              } catch (java.lang.SecurityException exc) {
                  System.err.println("Not permitted access to java.class.main system
      property.");
                  System.exit(1);
                  return;
              }
              if (mainClassName == null) {
                  System.err.println("No main class specified - too earlier version of
      Java?");
                  System.exit(2);
              }
              
              MyTestRunner.test(Class.forName(mainClassName));
          }
      }
      ---------- END SOURCE ----------

      CUSTOMER WORKAROUND :
      Write lots of main methods.

      Severly restrict the way tests are launched.
      (Review ID: 181732)
      ======================================================================

      Name: rmT116609 Date: 03/06/2003


       DESCRIPTION OF THE PROBLEM :
      We need to determine the name of the class that was specified on the java command line ('main class').

      Background: The main method is PUBLIC static void, so it is 'inheritable'. That implies there should be no need to override main and call super(args) in each subclass if the implementation differs. However, since the super main
      doesn't have access to the name of the main class, it's pointless anyhow.

      If the main class name (as specified on the command line) could be read from system properties, this would get around this issue.




      (Review ID: 180704)
      ======================================================================

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              nthompsosunw Nathanael Thompson (Inactive)
              Votes:
              0 Vote for this issue
              Watchers:
              6 Start watching this issue

                Dates

                Created:
                Updated:
                Imported:
                Indexed: