-
Loading classes at runtime
I am trying to write a Java program that accepts command line arguments which
specify classes to be loaded. Can I use the functions from the Class class
to do this? The program will not know the names of the classes until it
is run, however, it will know that all of the classes that it might encounter
extend a given class. For instance, on the command line the user will type:
java MyProgram A B (where A and B are classes that both extend class Letter).
Then, MyProgram's main function will create an instance of A and an instance
of B.
Any help you can give would be greatly appreciated.
Shayne
-
Re: Loading classes at runtime
soneill@mindspring.com (Shayne) wrote in <3a2c8703$1@news.devx.com>:
>
>I am trying to write a Java program that accepts command line arguments
>which specify classes to be loaded. Can I use the functions from the
>Class class to do this?
See Class.forName() and Class.newInstance().
http://www.google.com/search?as_q=fo...example&num=30
&btnG=Google+Search&as_oq=&as_epq=&as_eq=&as_occt=any&lr=&as_dt=i&sitesearc
h=&safe=off
Bill.
-
How Interesting...
I so happens that I was just researching the exact same thing. I found a pretty helpful article that explains a lot. You may also want to check out Sun's website.
Here is the link for the article Article for loading classes during runtime. Actually they're more like a series of articles I recommend reading them all.
Last edited by Radroach; 05-12-2007 at 01:01 AM.
Reason: Adding more information
-
Take a look at discussion for implementing the "Strategy" design pattern - this resolves the same challenge as your question. You write your functions in your client class(es) using the method calls and data fields held in the abstract class(es) or interface(s), then have several implementing classes which provide the different look or behavior you are indicating by your command line argument(s).
Here are two concise and informative descriptions of the implementation of Strategy:
http://java-x.blogspot.com/2006/12/i...n-in-java.html
http://www.javaworld.com/javaworld/j...npatterns.html
Last edited by nspils; 05-12-2007 at 08:22 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks