Determining the JVM's command-line root class
<p>When a subclass inherits <strong>main()</strong> from a superclass, is it possible to determine the actual class invoked on the command-line? For example, consider the following two classes, in which <strong>main</strong> is implemented by <strong>A</strong> and inherited by <strong>B</strong>:</p>

<pre><code>public class A {

    public static void main(String[] args) throws Exception {
        // Replace with &lt;some magic here&gt; to determine the class 
        //    invoked on the command-line
        final Class&lt;? extends A&gt; c = A.class;
        System.out.println("Invoked class: " + c.getName());

        final A instance = c.newInstance();
        // Do something with instance here...
    }
}

public class B extends A {
}
</code></pre>

<p>We can invoke <strong>B</strong> successfully (i.e., <strong>B</strong> does 'inherit' <strong>main</strong> - at least in whatever sense static methods can be inherited), but I have not found a method to determine the actual class invoked by the user:</p>

<pre><code>$ java -cp . A
Invoked class: A

$ java -cp . B
Invoked class: A
</code></pre>

<p>The closest I've come is to require that the subclass implement <strong>main()</strong> and call a helper method in the superclass, which then reads the thread stack to determine the calling class:</p>

<pre><code>public class AByStack {

    public static void run(String[] args) throws Exception {
        // Read the thread stack to find the calling class
        final Class&lt;? extends AByStack&gt; c = (Class&lt;? extends AByStack&gt;)
            Class.forName(Thread.currentThread().getStackTrace()[2].getClassName());
        System.out.println("Invoked class: " + c.getName());

        final AByStack instance = c.newInstance();
        // Do something with instance here...
    }

    public static void main(String[] args) throws Exception {
        run(args);
    }
}

public class BByStack extends AByStack {

    public static void main(String[] args) throws Exception {
        // Call the master 'run' method
        run(args);
    }
}
</code></pre>

<p>This method works:</p>

<pre><code>$ java -cp . AByStack
Invoked class: AByStack

$ java -cp . BByStack
Invoked class: BByStack
</code></pre>

<p>But I'd really like to eliminate the requirement that subclasses implement <strong>main()</strong> (yes, call me picky...). I don't mind if it requires some ugly code, since it will be implemented once and buried in the base class, and I'm mostly interested in Sun/Oracle VMs, so I'd be willing to consider using a private sun.misc class or something similar.</p>

<p>But I do want to avoid platform-dependencies. For example, on Linux, we can look at <em>/proc/self/cmdline</em>, but that's of course not portable to Windows (I'm not sure about Mac OS - I don't have my Mac with me at the moment to test this trick). And I think JNI and JVMTI are out for the same reason. I might be wrong about JVMTI, but it looks to me like it would require a C wrapper. If not, perhaps we could use that interface somehow.</p>

<p>This question was asked years ago at <a href="http://www.coderanch.com/t/375326/java/java/Getting-command-line-class" rel="nofollow">http://www.coderanch.com/t/375326/java/java/Getting-command-line-class</a>. The best answer there required a static initializer block in each subclass - a different, but similar requirement on the subclass author to the <strong>main</strong> calling <strong>run()</strong> solution I demonstrated. But I haven't seen more recent discussions; I'm hopeful that current VMs might allow access to information that wasn't available at the time of that discussion.</p>
