United StatesChange Country, Oracle Worldwide Web Sites Communities I am a... I want to...
Bug ID: JDK-4827318 System property for main class
JDK-4827318 : System property for main class

Details
Type:
Enhancement
Submit Date:
2003-03-05
Status:
Resolved
Updated Date:
2013-05-10
Project Name:
JDK
Resolved Date:
2013-05-10
Component:
tools
OS:
linux,windows_2000
Sub-Component:
launcher
CPU:
x86
Priority:
P4
Resolution:
Not an Issue
Affected Versions:
1.3.1,1.4.1
Fixed Versions:

Related Reports
Relates:

Sub Tasks

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)
======================================================================

                                    

Comments
EVALUATION

The launcher knows the name of the class whose main method is invoked, so
reporting this in a system property should not be too difficult.  The property
cannot always be defined; e.g., if the VM is created via the native invocation
API then there is no concept of a "main" class.  The specification of the
property should therefore allow this.

-- ###@###.### 2003/3/6
                                     
172-11-03 0
The goal and intent of this bug is to query the main-class used to launch an app, this can be
obtained from System.getProperty("sun.java.command") the same can also be obtained from a
running process, by using 
% jinfo $pid | grep sun.java.command



                                     
2013-05-10



Hardware and Software, Engineered to Work Together