This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes How to run a class who’s name is unknown in advance? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to run a class who’s name is unknown in advance? " Watch "How to run a class who’s name is unknown in advance? " New topic
Author

How to run a class who’s name is unknown in advance?

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
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

Crazy Bikes created by m-Power
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60765
    
  65

This is a job for (fanfare) Java Reflection!
Check out the Class.forName() method, and the Class.newInstance() method.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks bear.
I looked into Class.forName(). Seems I need to make all classes extend an interface or superclass to make this dynamic invoke work.
But I don’t see why I need reflection.
Steve Deadsea
Ranch Hand

Joined: Dec 03, 2001
Posts: 125
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.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
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 ]
David Duran
Ranch Hand

Joined: Feb 11, 2002
Posts: 122
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 ]
Adam Blinkinsop
Greenhorn

Joined: Mar 22, 2002
Posts: 7
David's right. Here's some example code:

So yeah. A little reflection (great stuff), and lots of try-catch blocks. All of which is in the JavaDocs! (*tear*)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to run a class who’s name is unknown in advance?
 
Similar Threads
Running a .bat/ .cmd file from Java
executing scripts
How to check application running on windows machine using java
What Class.forName() does?
trying to run a linux commands from a java app