java - Determining the JVM's command-line root class -
when subclass inherits main() superclass, possible determine actual class invoked on command-line? example, consider following 2 classes, in main implemented a , inherited b:
public class { public static void main(string[] args) throws exception { // replace <some magic here> determine class // invoked on command-line final class<? extends a> c = a.class; system.out.println("invoked class: " + c.getname()); final instance = c.newinstance(); // instance here... } } public class b extends { } we can invoke b (i.e., b 'inherit' main - @ least in whatever sense static methods can inherited), have not found method determine actual class invoked user:
$ java -cp . invoked class: $ java -cp . b invoked class: the closest i've come require subclass implement main() , call helper method in superclass, reads thread stack determine calling class:
public class abystack { public static void run(string[] args) throws exception { // read thread stack find calling class final class<? extends abystack> c = (class<? extends abystack>) class.forname(thread.currentthread().getstacktrace()[2].getclassname()); system.out.println("invoked class: " + c.getname()); final abystack instance = c.newinstance(); // instance here... } public static void main(string[] args) throws exception { run(args); } } public class bbystack extends abystack { public static void main(string[] args) throws exception { // call master 'run' method run(args); } } this method works:
$ java -cp . abystack invoked class: abystack $ java -cp . bbystack invoked class: bbystack but i'd eliminate requirement subclasses implement main() (yes, call me picky...). don't mind if requires ugly code, since implemented once , buried in base class, , i'm interested in sun/oracle vms, i'd willing consider using private sun.misc class or similar.
but want avoid platform-dependencies. example, on linux, can @ /proc/self/cmdline, that's of course not portable windows (i'm not sure mac os - don't have mac me @ moment test trick). , think jni , jvmti out same reason. might wrong jvmti, looks me require c wrapper. if not, perhaps use interface somehow.
this question asked years ago @ http://www.coderanch.com/t/375326/java/java/getting-command-line-class. best answer there required static initializer block in each subclass - different, similar requirement on subclass author main calling run() solution demonstrated. haven't seen more recent discussions; i'm hopeful current vms might allow access information wasn't available @ time of discussion.
presumably want tool can called different names, behave largely same depending on name invoked with? or similar magic?
in case, have actual main() method in sub-classes , delegate method takes name of invoked class:
public class super { protected void domain(string invokee, string... args) { system.out.println("i invoked as: " + invokee); } } public class toola { public static void main(string... args) { new super().domain("toola", args); // or toola.class.getname() refactor-proof } }
Comments
Post a Comment