Loading classes at runtime


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4

Thread: Loading classes at runtime

  1. #1
    Shayne Guest

    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

  2. #2
    Bill Davis Guest

    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.

  3. #3
    Join Date
    May 2007
    Posts
    1

    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

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center