0

I have a Java program that is run in Command Line, that contains a set of classes that I have written which all contain common code elements.

Lets assume the structure is the following:

package.AbstractParent  (contains: static void main)
package.ClassA          (extends: AbstractParent)
package.ClassB          (extends: AbstractParent)

When I do the following: java package.ClassA, the static void main within the AbstractParent is called.

Without putting a static void main in ClassA and ClassB, is there a way that I can determine which was called within AbstractParent?

For example, when I run java package.ClassA, AbstractParent will determine ClassA. When I run java package.ClassB, AbstractParent will determine ClassB.

topherg
  • 4,203
  • 4
  • 37
  • 72
  • Do you mean this (http://stackoverflow.com/questions/6271417/java-get-the-current-class-name)? – wake-0 Jun 16 '16 at 12:24
  • Not quite, as that doesn't work from Static context – topherg Jun 16 '16 at 12:28
  • >Just use TheClassName.class instead of getClass(). (http://stackoverflow.com/questions/8275499/how-to-call-getclass-from-a-static-method-in-java) – wake-0 Jun 16 '16 at 12:29

1 Answers1

0

Found the answer fairly quick, but Googling was troublesome, so I'll leave this here.


In the end, I was able to use System.getProperty("sun.java.command") to resolve the class. However, it does contain all the subsequent arguments that are included:

className = System.getProperty("sun.java.command");
className = className.substring(0, className.indexOf(" "));

From either context, it returned either package.ClassA or package.ClassB

topherg
  • 4,203
  • 4
  • 37
  • 72