The name of the class is assigned to a variable classNmae at runtime. How to run this class? Assume the class is started by run() method. 1) Can I use: className aObj = new className(); aObj.run(); 2) Can I use String cmd = "java " + className; Runtime rt = Runtime.getRuntime(); pr = rt.exec(cmd); Thanks [ March 21, 2002: Message edited by: Bruce Jin ]
BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design
You can do it with reflection even if you don't want to extend another class or implement an interface. With reflection you could query the class to see what methods it has and choose one of them to run. Or you could see it it has a main method and run that.
Joined: Sep 20, 2001
How this can be done (using reflection)? Foe example, I use reflection and find out there is a main method in my class Example, how do I invoke it after loading the Class? Class c = Class.forName("Example"); Thanks [ March 22, 2002: Message edited by: Bruce Jin ]
I've never used reflection so I'm not too sure on the syntax for the usage of these methods but you might want to look at: getMethod(...) in class Class & invoke(...) in class Method [ March 22, 2002: Message edited by: David Duran ]