Prev: in the Observer (Publish/Subscribe) pattern, are notificationsalien method calls?
Next: Using 'final' with local variables
From: Ross on 14 Sep 2009 06:26 I would like to write a base class from which child classes inherit a public static void main( String args[] ) method. As readers can probably guess, but problem is that the main method doesn't know which class to create to start the program running. The parent class is called Game. I can't write a method: public static void main( String args[] ) { Game g = new Game(); g.setVisible( true ); } as it runs the base class. At present I have a method (simplified, from memory) which takes the name of the class as a runtime argument. E.g. public static void main( String args[] ) throws Exception { Class c = Class.forName( args[0] ); Game g = (Game) c.newInstance(); g.setVisible( true ); } This works, but requires the name of the class to be given as an argument. If I could find out the name of the class that had been executed, then I could solve this problem. But I've looked in the System class properties, I've looked in Runtime, and I haven't found anything. Is there a solution to this problem? I would like the solution to be cross-platform.
From: Roedy Green on 14 Sep 2009 07:23 On Mon, 14 Sep 2009 03:26:07 -0700 (PDT), Ross <rossclement(a)gmail.com> wrote, quoted or indirectly quoted someone who said : >I would like to write a base class from which child classes inherit a >public static void main( String args[] ) method. You naturally inherit all static methods, including main. You can specify which generation you want by prefixing the class name. e.g. Grandma.main Mom.main Me.main If there is no Me.main, you can use Me.main to get an Mom.main. -- Roedy Green Canadian Mind Products http://mindprod.com "The telephone is the greatest single enemy of scholarship; for what our intellectual forebears used to inscribe in ink now goes once over a wire into permanent oblivion." ~ Dr. Stephen Jay Gould (born: 1941-09-10 died: 2002-05-02 at age: 60)
From: Andreas Leitgeb on 14 Sep 2009 08:00 Roedy Green <see_website(a)mindprod.com.invalid> wrote: >> I would like to write a base class from which child classes inherit a >> public static void main( String args[] ) method. > You naturally inherit all static methods, including main. > You can specify which generation you want by prefixing the class name. > > e.g. Grandma.main Mom.main Me.main > If there is no Me.main, you can use Me.main to get an Mom.main. The real question was (as far as I understood it): Can the implementation of Mom.main() find out, whether the user used "Mom" or "Me" as startup class? Or perhaps even "Brother" or "Sister", which for the sake of this example also do not have their own main() but fall back to Mummy's? I don't know any way, and I doubt there even is one, but can't say for sure. Perhaps Java can be asked for the startup-class directly?
From: Alessio Stalla on 14 Sep 2009 09:30 On Sep 14, 2:00 pm, Andreas Leitgeb <a...(a)gamma.logic.tuwien.ac.at> wrote: > Roedy Green <see_webs...(a)mindprod.com.invalid> wrote: > >> I would like to write a base class from which child classes inherit a > >> public static void main( String args[] ) method. > > You naturally inherit all static methods, including main. > > You can specify which generation you want by prefixing the class name. > > > e.g. Grandma.main Mom.main Me.main > > If there is no Me.main, you can use Me.main to get an Mom.main. > > The real question was (as far as I understood it): > > Can the implementation of Mom.main() find out, whether the user > used "Mom" or "Me" as startup class? Or perhaps even "Brother" > or "Sister", which for the sake of this example also do not have > their own main() but fall back to Mummy's? The real answer is: even if it can, it shouldn't. The OP should really refactor the code. If a custom launch protocol is desired, write it explicitly. Something as: java my.own.Game -game PacMan (searches for class PacMan in default package my.own.games and invokes main() or whatever) java my.own.Game -game org.foobar.FooTetris (searches for fully qualified class org.foobar.FooTetris and does as above) then if that's too verbose you can hide it in a (platform specific) shell script. Alessio
From: Leif Roar Moldskred on 14 Sep 2009 09:36 Ross <rossclement(a)gmail.com> wrote: > This works, but requires the name of the class to be given as an > argument. If I could find out the name of the class that had been > executed, then I could solve this problem. But I've looked in the > System class properties, I've looked in Runtime, and I haven't found > anything. I think you're asking for something like this: Thread mainThread = Thread.currentThread(); StackTraceElement[] currentStack = mainThread.getStackTrace(); StackTraceElement mainStackElement = currentStack[ currentStack.length - 1 ]; System.out.println( "My main class is " + mainStackElement.getClassName() ); -- Leif Roar Moldskred
|
Next
|
Last
Pages: 1 2 3 4 5 6 Prev: in the Observer (Publish/Subscribe) pattern, are notificationsalien method calls? Next: Using 'final' with local variables |