DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 4 of 4
  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.


  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().



  3. #3
    Join Date
    May 2007

    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
    San Bernardino County, California
    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:


    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
Latest Articles
Questions? Contact us.
Web Development
Latest Tips
Open Source

   Development Centers

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

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.