Prev: in the Observer (Publish/Subscribe) pattern, are notificationsalien method calls?
Next: Using 'final' with local variables
From: Ross on 15 Sep 2009 02:24 On Sep 15, 1:06 am, Arne Vajhøj <a...(a)vajhoej.dk> wrote: > If we can assume that Java version >= 1.5, main thread has > thread id 1 and max. stack depth is 1000: > > StackTraceElement[] ste = > ManagementFactory.getThreadMXBean().getThreadInfo(1, 1000).getStackTrace(); > String clznam = ste[ste.length-1].getClassName(); > > Arne That doesn't work, it always gives the name of the class where main is declared, not the subclass.
From: Ross on 15 Sep 2009 02:36 On Sep 15, 4:04 am, Mike Amling <maml...(a)rmcis.com> wrote: > Could you change that boilerplate to invoke a constructor in Game via an > implicit super(), and have the Game constructors leave what you need in > one of Game's static variables where Game.main can find it? > > class Mine extends Game { > static {new Mine();} > ... > > } I could do that, but I think it's more confusing than what I'm going now. I've thought about this a bit, and had one go at getting the child to program in the language. The biggest problem was forgetting to put semicolons after commands. Because while "proper java" is being written, methods from the Game class are being used that makes it look like a "Logo" like language. Because what is being written is limited, it will be easy to write code into the IDE that looks for common errors and gives human-like advice. Hence, I'll modify the IDE anyhow, and making my initial solution work (give the name of the class as an argument) is easy. The IDE I'm using is a very basic IDE written by a colleague of mine at work. I have all the source code.
From: Arne Vajhøj on 17 Sep 2009 21:30 Ross wrote: > On Sep 15, 1:06 am, Arne Vajh�j <a...(a)vajhoej.dk> wrote: >> If we can assume that Java version >= 1.5, main thread has >> thread id 1 and max. stack depth is 1000: >> >> StackTraceElement[] ste = >> ManagementFactory.getThreadMXBean().getThreadInfo(1, 1000).getStackTrace(); >> String clznam = ste[ste.length-1].getClassName(); > > That doesn't work, it always gives the name of the class where main is > declared, not the subclass. True. Mike Schilling already pointed that out. Arne
From: Arne Vajhøj on 17 Sep 2009 21:30 Mike Schilling wrote: > Arne Vajh�j wrote: >>>> If we can assume that Java version >= 1.5, main thread has >>>> thread id 1 and max. stack depth is 1000: >>>> >>>> StackTraceElement[] ste = >>>> ManagementFactory.getThreadMXBean().getThreadInfo(1, >>>> 1000).getStackTrace(); String clznam = >>>> ste[ste.length-1].getClassName(); >>> Is that really going to give you the class mentioned in the command >>> line rather than the class which defines the static main() method? >> You are correct. It returns the parent class where main is >> defined. > > Right. My strong impression is that trying to figure out which subclass was > mentioned in the command line is simply hopeless (or at best extremely > non-portable) and that, accordingly, the OP should figure out some other way > of doing things. Fortunately, there are dozens of them. True. JNI and an OS specific call is not a good solution. Arne
From: Toffstier on 5 Oct 2009 06:41
On 15 Sep., 03:32, "Mike Schilling" <mscottschill...(a)hotmail.com> wrote: > Arne Vajhøj wrote: > >>> If we can assume that Java version >= 1.5, main thread has > >>> thread id 1 and max. stack depth is 1000: > > >>> StackTraceElement[] ste = > >>> ManagementFactory.getThreadMXBean().getThreadInfo(1, > >>> 1000).getStackTrace(); String clznam = > >>> ste[ste.length-1].getClassName(); > > >> Is that really going to give you the class mentioned in the command > >> line rather than the class which defines the static main() method? > > > You are correct. It returns the parent class where main is > > defined. > > Right. My strong impression is that trying to figure out which subclass was > mentioned in the command line is simply hopeless (or at best extremely > non-portable) and that, accordingly, the OP should figure out some other way > of doing things. Fortunately, there are dozens of them. public static String getMainClassName() { for(final Map.Entry<String, String> entry : System.getenv().entrySet ()) { if(entry.getKey().startsWith("JAVA_MAIN_CLASS")) return entry.getValue(); } throw new IllegalStateException("Cannot determine main class."); } |