Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting command line class

 
James Davies
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to get the class that was called at the command line? I have a superclass with several subclasses that inherit its main. I want to be able to determine which class was called at the command line so I can create an instance of that class. Here is an example of what I am trying to do:
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can't inherit main -- it's static. static methods do not participate in inheritance. Therefore, you know that the class that is running main was the class that was entered at the command line -- unless, of course, you called main from another class (which is permissible...main is just another method, after all).
 
James Davies
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't want to be rude, but your answer is completely wrong. Static method are inherited and main can be inherited as well. Want proof? try this simple example. Create a file called mainTest.java with this code and compile it and run it as "java mainTest":

[ December 06, 2004: Message edited by: James Davies ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you cannot override static methods... They ARE inherited but when you create a new method with the same signature they hide (mask) the original rather than override it.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java can be invoked in a wide variety of ways, not just via the command-line "java" program. The idea that the static method "main" of some specified class is the entry point of the application is part of the "java" program's interface, not something fundamental to Java.

Perhaps that's why there is no concept of the applications's main class in the Java API. Whatever the reason, I don't think there's a built-in way to find out what class was run by the "java" command.

How much flexibility do you have? Could you always run the same class (your superclass), but provide the name of the subclass to be instantiated via one of the command-line arguments or via a system property? Could you write your own invoker program, using JNI invocation interface? That would free you completely from the constraints of "java" program, but is not trivial, so you'd need to be sure it was necessary.
 
S. Sundar Raman
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can get this work if you define a static variable that holds the reference to the Class object of the Class that is invoked (Better understood by reading the source provided below. I have not considered cases on the real usability of the code. One can fine tune them as necessary.)

SuperClass.java :


MainTest.java :

 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by S. Sundar Raman:
You can get this work if you define a static variable that holds the reference to the Class object of the Class that is invoked (Better understood by reading the source provided below. I have not considered cases on the real usability of the code. One can fine tune them as necessary.)


Well, that works and is quite clever. But I was assuming you can't add anything to the subclasses (you have added a static initialiser block). If you can add things to the subclass, why not do it the obvious way and put a main() in the subclass? The subclass main() would then call a method (called main() or something else) in the superclass, passing in whatever information is needed.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic