Win a copy of Practical SVG this week in the HTML/CSS/JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Question of class loaders.

 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

We use a propritory EJB Server of our company and facing following problem in loading class across packages.

We have two EJB applications AppA and AppB and we want to extend ClassB of AppB from AppA.

When I try this. (assume that package names of apps are AppA and AppB)



I am getting ClassNotFoundException for AppB.ClassB.

But when I try like this using encapsulation and delegation.



I think this is related to class loaders. Basically it is not able to find the class if its part of "extends ClassName", but it works when I use "new" to create an instance.

Can any one able to explain reason for this behaviour..?

Thanks,
E. Rajasekar.
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rajasekar,

public class ClassB extends AppB.ClassB

Are by any chance ClassB and AppB.ClassB ejbs, or are they just POJOs?
Regards.
 
Rajasekar Elango
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Valentin,

They are Plain Old Java Objects..

Thanks,
Rajasekar
[ March 21, 2005: Message edited by: Rajasekar Elango ]
 
Valentin Tanase
Ranch Hand
Posts: 704
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please specify several of more things:
  • Did you pack the applications the same way in both cases (when using inheritance vs encapsulation and delegation)? Was the system classpath the same, when you testing each scenarios? Was the runtime environment the same?
  • How do you pack the AppA and AppB (in both cases if they are different).
  • What packing strategies you currently use (adding the class to the system classpath, using common libraries, etc), in order to make sure that AppA can import the ClassB from AppB?

  •  
    Rajasekar Elango
    Ranch Hand
    Posts: 105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,

    In our EJB Server, all classes needed by an application can be within same application (inside correct package structure) or can be in jars files under lib directory or if it is n another application, we had to set a variable PACKAGE_DEPENDIES with dependent paths which I had set correctly. CLASSPATH variable will be ignored.

    I basically want to know how classes are identified when it is part of "extends classname" and if its created using "new" operator.

    The same problem occurs with interfaces also, ie if is give "implements interfacename" it throws error and if i use "new" to create a anonymous class implementing the interface, it works.

    Thanks,
    E. Rajasekar.
     
    Valentin Tanase
    Ranch Hand
    Posts: 704
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    In our EJB Server, all classes needed by an application can be within same application (inside correct package structure)

    Correct.

    or can be in jars files under lib directory

    I would suppose you have an external (global) lib folder from where your server loads the libraries. In that case the libraries will be appended to the system classloader and this is not a very good practice. The direct consequence of such approach is that the hot deployment feature won�t work. The server needs to be restarted all the time when the common libraries changes (the server has no other way to load the new versions).

    or if it is n another application, we had to set a variable PACKAGE_DEPENDIES with dependent paths which I had set correctly. CLASSPATH variable will be ignored.

    Oooops, you got me with this one. This doesn�t looks to me like a common or standard J2EE practice. Please correct me if I�m wrong or I misunderstood you. If you have a class A in an ear/war archive and you want to use the same class A into another ear/war, you have two choices (or alt leas I didn�t hear about any others):
  • Add the class A to the system classpath, which is not a very good practice.
  • Add the class A to both ear/war files, which is the common practice.


  • You might reconsider your packing strategies. You might also consider revisiting your container�s classloader architecture.

    I basically want to know how classes are identified when it is part of "extends classname" and if its created using "new" operator.

    The same problem occurs with interfaces also, ie if is give "implements interfacename" it throws error and if i use "new" to create a anonymous class implementing the interface, it works.

    Again I might not understand you very clear, but from the classloading perspective it is no difference how you use your class: extending it or instantiate objects of that type. All it cares about is the import directive. Once you add this line:

    Or by using the full qualified name of the class, the classloader will try to load the class you�ve imported. If it is not found, then a ClassNotFoundException will be thrown. The question you need to answer is how your container loads the classes. Weblogic for example creates a parent-child classloader relationship. Foe each ear it will define a system classloader (loads the classes in system classpath), an application classloader (loads classes in war) and an EJB classloader (loads the ejbs). Two different application are considered to be loaded by two completely (unrelated) classloader and therefore it is no way for them to share common classes (except through the system classloader).
    Regards.
     
    Rajasekar Elango
    Ranch Hand
    Posts: 105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi valentin,

    Thanks a lot for all these explanations and now I understand how class loading takesplace in ejb apps.

    I will contact our EJB Server support team to know how it works in our server to clarify my question.

    Thanks Again,
    E. Rajasekar.
     
    Valentin Tanase
    Ranch Hand
    Posts: 704
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You're very welcome Rajasekar
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!