Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Class Loader, Common problem, Help!!!

 
Peter Kovgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know it is a very common problem, but I can't find any complete answer for the question.
Please help me!
I am developing some application on some j2e platform, when I 'm doing lookup(from servlet, from bean or from jsp ,using some helper class) :
SomeClass c = (SomeClass)initCtx.lookup("name");
or by another method:
Object obj = initCtx.lookup("name");
SomeClass c = (SomeClass )PortableRemoteObject.narrow(obj,SomeClass .class);
lookup failed with ClassCastException
I know exactly what a problem: server loads SomeClass by particular class loader(CL1) and when I call
SomeClass from client another class loader(CL2) used.
I can't change class loader by which loaded object on server, then I must use anywhere CL1 to achieve
right casting.
How to do it?
I tried use Class.forName("SomeClass ",true,CL1);
I tried use CL1.loadClass("SomeClass")
but with these methods I only can receive Class object or createInstance(), but I can't keep reference to remote object.
What can I do to establish good casting???
Please, help!!!
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18277
56
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Casting won't help. Aside from the basic fact that the classes are loaded into different contexts, I think there are basic security issues that keep you from having classes from 2 different loader spaces work like that.
The solution is to place the class into a place where a copy can be loaded by the class context where you're calling. Usually this can be done by adjusting the classpath. Sometimes you have to move or copy files .
Because this is such a common - and frustrating problem, most appservers now clearly document what classloader constructions are used in the various differing parts of the system. When in doubt, consult the map.
 
Peter Kovgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Tim!
Thank you for that idea.
-----------------------------------You write...
The solution is to place the class into a place where a copy can be loaded by the class context where you're calling. Usually this can be done by adjusting the classpath. Sometimes you have to move or copy files .
*******************************************
1. Which class I should place into particular place? The jar, which includes
lookup(ed) object?
I don't know where I should place my jar? Into server's lib directory?
Please, some explanation!!!
Thank you!!!
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please don't post the same question in multiple places, it creates duplicate conversations. I'm going to ask for the other copy to be closed.
It would help if you said which app server you were using, there is a difference in the way their CassLoader heirarchies are arranged.
Dave
 
Peter Kovgan
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using SAP's WAS.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic