?
Solved

ClassLoader / Threads etc.

Posted on 2003-03-31
37
Medium Priority
?
333 Views
Last Modified: 2013-12-29
Hi,

I am trying to use MyCustomClassLoader to load java classes (my app is running under WebLogic).

Right now I am facing two problems I don't know what to do about:

1. I have tried to load classes through MyCustomClassLoader and all of them got loaded, but when it's turn to load java.lang.Object, a SecurityException is thrown saying "prohibited package name: java/lang". I have read it somewhere that I will not be able to load java.* package. Is that true? Is there a way around it?

2. While my classloader is running fine (except for the problem # 1), I am still trying to make it the default classloader for my app. The approach I chose (and tell my if this approach is wrong) is to make MyCustomClassLoader the default one for the threads in the threadpool serving my app (ExecuteThread 'number' for the application 'my application name'). I change all of them, verify that they have been changed, launch my first servlet, chech them again and notice that threads used to load my servlet have been changed to sun.something.something (the default classloader). What am I doing wrong? Is the approach I chose wrong?

I will give you 500 and an A grade if you help me make it work.

Thanks
0
Comment
Question by:mkaplunov
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 14
  • 8
  • 6
  • +5
37 Comments
 
LVL 3

Expert Comment

by:wide_awake
ID: 8242740
My understanding of the class loader architecture is that it uses a sort of delegation model to load classes.  It delegates the class loading to the class loader closes to the root ni the hierarchy (and therefore most trusted one) to actually load the class.  When you make your own class loader, it will only load classes that can't be loaded by the built-in classloaders.  

The reason for this is security.  If you could force the JVM to use your class loader, you would basically have total control over security.  The model they use means that the most trusted classloader will load the bootstrap classes, ensuring that security is not compromised.

It's been a while since I've messed around with classloaders, so I may be wrong :)
0
 
LVL 92

Expert Comment

by:objects
ID: 8243188
1. I don't think your custom classloader should be loading system classes.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8243226
You may be right, but I would like to know if it is possible at all.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 9

Expert Comment

by:Venci75
ID: 8243601
1. yes - loading java.* classes causes SecurityException and there is no way to around this. Why do you want to load java.lang.Object? It is already loaded by the system class loader and you don't need to do this?
2. instead of trying to load your servlet with a custom class loader - try to move the servlet functionality to another class and load this class using your class loader.

0
 
LVL 2

Expert Comment

by:nir2002
ID: 8244048
Hi,

>>I am trying to use MyCustomClassLoader to load java classes (my app is running under WebLogic).

If this is in EJB, this situation is illegal by the EJB spec.

Best regards
Nir
0
 
LVL 35

Expert Comment

by:girionis
ID: 8244404
 1) As already stated it is not allowed since the system classes are already loaded (if they weren't you couldn't run your application :-) ). Loading them again could cause serious problems to the VM (for instance you could end up with two instances of static variables) hence it is not allowed and there isn't any workaround AFAIK.

  2) Again Servlets are loaded by a different classloader. Classloaders always ask the parent classloader up to the hierarchy tree (and the parent classloader delegates the request to the "grandparent" classloader etc) to load a class and only if a) the parent classloder cannot load the class and b) the class is not already loaded, then the classloader is allowed to load a class. If you already have written a custom classloader I suggest you move it up to the hierarchy tree and see (since, hopefully, the default Servlet classloader will ask your custom classloader to load the Servlet classes) if this works. If you could "override" WebLogic's default Servlet classloader then your custom classloader will precede it and will be able to load the classes. If you haven't already done so you might want to take a look here: http://edocs.bea.com/wls/docs81/programming/classloading.html
0
 

Expert Comment

by:itzsuresh
ID: 8244903
1. almost all posts above have answered this question: you cannot and must not load java* classes. if u are allowed to do this, then it is a severe security problem, coz someone else can write a java.lang.Object class with a malicious intent and if that is loaded, then u are in all sorts of problems.

2. try to remove the classes that you would like to load thru your classloaded from the classpath. this makes it inaccessible to the default classloader. now try to load these classes from a fixed location thru ur classloader. i think this could solve your problem.

regards,
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8247670
Venchi75:
I am trying to decrypt my class files, so I have to use my customised class loader

nir2002:
I agree

girionis:
I have read that document and as far as I understand it shows one how to specify a custom classloader's hierarchy, and not how to include one's custom classloader. Do you know how to move MyCustomClassLoader up the hierarchy?

itzsuresh:
Regarding #2 - that's exactly what I am doing. None of the classloader can locate requested APIs, so MyCustomCLassLoader gets to load them. The only problem here, I need to make my ClassLoader the default one. Otherwise you can find yourself in a situation where you have to go through your entire application (and we have lots of files) and change every instance of all of your classes to be instantiated through MyCustomClassLoader...
0
 
LVL 92

Expert Comment

by:objects
ID: 8249687
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8249797
I don't know if I should laugh or cry, but I have read the same documents (almost all of them and much more), before posting this question...
0
 
LVL 92

Expert Comment

by:objects
ID: 8249840
Have you had a look at the tutorial on ibm's site?
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8249931
I found this tutorial in a PDF file on some other website
0
 
LVL 35

Expert Comment

by:girionis
ID: 8252071
>  Do you know how to move MyCustomClassLoader up the hierarchy?

  No sorry, don't know. The thing is that if none of the classloaders can load the requested APIs then the only one that can is your custom classloader so there might not be the need to put it up the hierarchy tree, because they will be loaded by your own classloader anyway.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8252145
girionis:

True. I could exclude the location of my class files from the CLASSPATH, but then I'd be back to the square one (loading classes by implicitly calling my class loader every time a class has to be loaded) and it’s gonna be very ugly and not very scalable...
0
 
LVL 35

Expert Comment

by:girionis
ID: 8252169
 That's how things work with the default classloaders as well. A class is loaded on demand. What you could do if you wanted is to load all of your custom classes at once and then they will be available if another class requests them.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8252223
I have though about it and I don't think it will work. If my custom classes are are loaded by my custom classloader and then requested/referenced by some other classes later on, they will be reloaded by the default classloader...


0
 
LVL 92

Expert Comment

by:objects
ID: 8252249
I'm confused, where is your classloader currently in the hierarchy, and which way are you referring to as up?
In fact how are you currently using your class loader to load classes?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8252276
 This could happen with any classes. The only way to make sure that a class will not be loaded twice is if it is loaded by the bootstrap classloader. If classloaders *above* your custom classloader do not request your classes then they are guaranteed that they will never be loaded twice. And frankly, I do not thing they will, it is like telling me that the String class, or the HttpServlet calss could make a call to your custom classes.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8252296
Remember I said I was running my app on WebLogic? There are multiple classloaders created (by WebLogic). Each threadgroup has a default classloader assigned to it, depending on the context served by the threads in this group.
0
 
LVL 2

Expert Comment

by:nir2002
ID: 8252325
Do your classes compiled with the WebLogic compiler?
if not try to compile it with them and let me know what happen.
Which verson of WebLogic do you use?

best regards
Nir
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8252355
girionis:

You're right that String would never call my class, but if class A was loaded by MyCustomClassLoader and then class B references/instantiates A, then the default class loader for this threadgroup is going to pick it up and then:

1. Check if it has loaded A before, and if it hasn't
2. Delegate class A up the hierarchy, if if neither of its parents can load A
3. Try to load A

MyCustomClassLoader is not a member and cannot be "inserted" into the classloader hierarchy for my app (default WebLogic ClassLoaders hierarchy). Am I wrong here?
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8252362
WL6.1 SP4, JDK1.3.1
I am using javac that came with the WL
0
 
LVL 2

Expert Comment

by:nir2002
ID: 8252406
How and when you use the class loader?
0
 
LVL 35

Expert Comment

by:girionis
ID: 8252450
 mkaplunov:

  I am not sure how WebLogic classloading works but, your classloader, should follow the delegation model like all java classloaders do (apart from bootstrap). If you look at the classloading document of WebLogc you will see that the weblogic-application.xml file has a <classloader-structure> element. Maybe you could use that to move your classloader up or down, but I am not sure if it can be done.

  As for your other question, it depends where class B is. If class B is loaded by some other classloader then, yes, you will end up with class A loaded two times. For this to happen class B needs to be in a place that the rest of the classloaders have access. If class B is anywhere else then you shouldn't have problems since the only classloder that could access it is your custom classloader.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8255359
nir2002:
*I am trying to make MyCustomClassLoader the default one for my app*, so I am trying to find out how and when it should be used.

girionis:
<class-loader> structure cannot be used, as it defines the structure but doesn't allow you to specify which custom classloader, if any, to use for a particular component.

If class B is available to MyCustomClassLoader only, then if I don't call the latter explicitly, I will get a ClassNotFoundException. I cannot add MyCustomClassLoader to the tree of the classloaders used, I just create a new branch and therefore if this branch is not made dafault for my app, it will never be used.

0
 
LVL 35

Expert Comment

by:girionis
ID: 8256776
> In fact how are you currently using your class loader to load classes?

  Objects asked a very valid question. Can you give us some more information?
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8256913
Right now I have a final class, let's call it class A, which is launched at startup. A has a static method called getClassLoader, which instantiates an instance of MyCustomClassLoader, so whenever I need to load a class, I do something like this:

Class toLoad = A.getClassLoader().loadClass("classname", boolean);
0
 
LVL 9

Expert Comment

by:Venci75
ID: 8259259
hm... I thought that the startup classes are loaded by the system class loader.
If you use a startup class - you can execute this:
Class.forName("servlet class", myCustomClassLoader);

unfortunately - if you want to re-deploy your application after that - you will have to restart the server

0
 
LVL 35

Expert Comment

by:girionis
ID: 8259722
 mkaplunov:

  Your classloader is alerady loaded so it can load any classes that can't be loaded by any other classloaders. As long as these classes reside in a location other than the default ones or are not acquired by other classes loaded by the default classloaders then there really shouldn't be any problem.

  Have you actually tried the system and you saw that there are issues?
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8263131
girionis:

Yes, I have. As I said before just because MyCustomClassLoader is loaded and the rest of classloaders doesn't know where my class files are, doesn't mean that the job of loading my classes will be delegated to MyCustomClassLoader (unless I explicitly call MyCustomClassLoader to load my classes). Remember that classloader hierarchy "talks" one way only (up the chain) and I can't make MyCustomClassLoader to be the parent of an existing AppClassLoader, which is the default one for my app.
0
 
LVL 92

Expert Comment

by:objects
ID: 8264223
You don't need to be telling other classloader about yours, your classloader needs to be the top (or bottom depending on semantics) of the chain, and it needs to be delegating to the default class loader.
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8264640
Do you know how to make it the top of the chain?
0
 
LVL 92

Expert Comment

by:objects
ID: 8264747
for an app I do, but haven't used wl.
Saw some doc on a later version of wl, but not for 6.1
0
 
LVL 35

Expert Comment

by:girionis
ID: 8268042
 mkaplunov:

  Top level calssloaders will not delegate to your classloader if it is below them. You can only delegate a request to the classloaders above.  

  I have just gone through the WebLogic documentation briefly. What I noticed is this:

  "You can create custom classloader hierarchies for an application allowing for better control over class visibility and reloadability"

  and this:
 

  "The top-level element in weblogic-application.xml includes an optional classloader-structure element. If you do not specify this element, then the standard classloader is used. Also, if you do not include a particular module in the definition, it is assigned a classloader, as in the standard hierarchy. That is, EJB modules are associated with the application Root classloader, and Web application modules have their own classloaders"

  Pay particular attention to the following line:

  "If you do not specify this element, then the standard classloader is used."

  My guess is that if you specify a classloader on the top-level element then the classloader you specified will be used for your application.

  Have you actualyl tried to use the <classloader-structure> element? Any luck?
0
 
LVL 1

Author Comment

by:mkaplunov
ID: 8271362
girionis:

Please, read my comments made on 04/02/2003 09:57AM PST
0
 

Expert Comment

by:modulo
ID: 8360528
Dear expert(s),

A request has been made to close this Q in CS:
http://www.experts-exchange.com/Community_Support/CleanUp/Q_20590122.html

Without a response in 72 hrs, a moderator will finalize this question by:

 - Saving this Q as a PAQ and refunding the points to the questionner

When you agree or disagree, please add a comment here.

Thank you.

modulo

Community Support Moderator
Experts Exchange
0
 

Accepted Solution

by:
modulo earned 0 total points
ID: 8373014
Saving this Q as a PAQ and refunding the 500 points to the questionner

modulo

Community Support Moderator
Experts Exchange
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question