Getting class using (Class.forname()) and casting the instance

Discussion in 'Java' started by Madni, Dec 19, 2005.

  1. Madni

    Madni Guest

    Dear All,

    This chunk of code is working well in my servlet

    Class myclass = Class.forName(strMgrApp);
    //where strMgrApp actually holds the class TagImportExportMgrApp
    TagImportExportMgrApp ob; // any class name
    ob = (TagImportExportMgrApp) myclass.newInstance();
    oImportExportMgr = (IImportExportMgr) ob;
    //IImportExportMgr is interface which TagImportExportMgrApp implements

    No runtime error is returned and i can access methods using object
    "oImportExportMgr"

    But !!!!!!!!!!!!!!!!!!!!!!!!!

    whats the problem with this line below :

    oImportExportMgr =(IImportExportMgr)
    Class.forName(strMgrName).newInstance();

    why it generates a runtime error stating :

    java.lang.IllegalAccessError: tried to access class
    WES.GenericComponents.UploadFile.IImportExportMgr from class
    WES.GenericComponents.UploadFile.UploadManager

    Any suggestion would be highly obliged

    Regards ,

    Madni
     
    Madni, Dec 19, 2005
    #1
    1. Advertising

  2. Madni

    Guest

    > java.lang.IllegalAccessError: tried to access class
    > WES.GenericComponents.UploadFile.IImportExportMgr from class
    > WES.GenericComponents.UploadFile.UploadManager


    Try posting a complete test case that we can compile and run, or
    examine the API docs for IllegalAccessError:
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IllegalAccessError.html

    A rebuild will probably fix this.

    Of course, you probably didn't paste the exception output properly, so
    you probably want to really look at:
    http://java.sun.com/j2se/1.5.0/docs/api/java/lang/IllegalAccessException.html

    Note that reflection is generally used to pretend that Java is a
    dynamically-typed language. The only valid use I know is for plugins
    that are not known about at the time the application is started.
     
    , Dec 19, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. learningjava
    Replies:
    1
    Views:
    557
    Anton Spaans
    Oct 27, 2003
  2. H.MuthuKumaraRajan

    .class and Class.forName

    H.MuthuKumaraRajan, Nov 6, 2003, in forum: Java
    Replies:
    11
    Views:
    1,785
    H.MuthuKumaraRajan
    Nov 7, 2003
  3. cyril
    Replies:
    2
    Views:
    3,947
    cyril
    Aug 25, 2004
  4. Ulrich Scholz
    Replies:
    9
    Views:
    8,450
    Chris Uppal
    Dec 7, 2005
  5. jean-francois
    Replies:
    2
    Views:
    624
Loading...

Share This Page