aspose file tools*
The moose likes Java in General and the fly likes Getting command line class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting command line class" Watch "Getting command line class" New topic
Author

Getting command line class

James Davies
Greenhorn

Joined: Mar 13, 2004
Posts: 4
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

Joined: Aug 20, 2001
Posts: 1821

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


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
James Davies
Greenhorn

Joined: Mar 13, 2004
Posts: 4
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.


Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
S. Sundar Raman
Greenhorn

Joined: Aug 12, 2004
Posts: 3
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

Joined: Oct 30, 2001
Posts: 1970
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Getting command line class