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

M

Madni

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
 
R

ricky.clarkson

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top