Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Getting class using (Class.forname()) and casting the instance

Reply
Thread Tools

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

 
 
Madni
Guest
Posts: n/a
 
      12-19-2005
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

 
Reply With Quote
 
 
 
 
ricky.clarkson@gmail.com
Guest
Posts: n/a
 
      12-19-2005
> 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/...cessError.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/...Exception.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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Don't understand behavior; instance form a class in another class'instance Martin P. Hellwig Python 1 03-26-2010 12:06 AM
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
converting base class instance to derived class instance Sridhar R Python 14 02-10-2004 02:47 PM
Cannot refer to an instance member of a class from within a shared method or shared member initializer without an explicit instance of the class. DJ Dev ASP .Net 3 02-08-2004 04:19 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57