• Post Reply Bookmark Topic Watch Topic
  • New Topic

ClassLoader quandary  RSS feed

 
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to load a class via classloader from a byte array. The byte array is downloaded from an Oracle database as a blob and converted. The file is good because when I output to a file it works and looks fine. The class implements an interface that is within the same package. I, and others on my team, have tried many different implemtations of classloader but we still get class not found exception. It seems to either not find the class or the interface that is implemented. Any help would be greatly appreciated..
 
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How are you going about loading the class - instantiating ClassLoader, and then calling defineClass(...)?

How have you ascertained that "the file is good"? If you save it to disk anmd then run javap on it, it should tell you which interfaces the class implements.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Correct, we are instantiating ClassLoader, and then calling defineClass. The file and interface also check out.

Thanks for the response..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Only code loaded by that special classloader would know about the class. So if the class trying to instantiate it was loaded by the system classloader, the behavior you observed would be expected.

Unless you can arrange to have all (or most) of your code loaded by the special classloader (which is unlikely), the only way out would be to write the class file into a directory that is covered by the regular classloader (WEB-INF/classes for a web app, or anything inside the CLASSPATH variable for a desktop app). Then the Class.forName call should work fine.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Thanks so much for the suggestion and input. I think you are right on the money. Let me work on this and I will let you know. Thanks again..
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

That did it. I was able to load the class and invoke the method. I need to now load the supporting jars on demand say at start up or when a jar is updated without bringing down the web application. Can you use classloader to load jars? I have seen code before but need to hunt some down. If you have any suggestions, please post.

Again, thanks so much..
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it's possible to load classes in jar files. Check the source code of the java.net.URLClassLoader class for how to do that.

Dealing with a changing jar file without reloading the web app is tricky, though. You need to make sure that there are no references to any of the classes loaded by the classloader used to load those classes, or to the classloader itself. Then they all become eligible for GC, and you have a chance to replace it with a classloader that uses the new version of the jar file.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

I have heard that reloading is dicey at best. I guess that will be in version 2.0 =) As for loading jars, I don't know if I need to load all the classes within a jar per se, I just need the jar to be accessible in case the class that I do load makes reference to it. I don't know if that means loading the jar via a URLClassLoader or physically downloading to somewhere within the classpath. Gonna try some things today to see what works.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ulf,

Actually, I am having an issue with the downloaded class not having any access to the classes within the main application. It is like there is a firewall between the downloaded class and the main application calling it. I had assumed that all classes, either in the main application or loaded classes, ran in the same space. Do you know how to make that connection between the two? I am running Eclipse with Tomcat.
[ September 04, 2008: Message edited by: James Daniel ]
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am having an issue with the downloaded class not having any access to the classes within the main application. It is like there is a firewall between the downloaded class and the main application calling it. I had assumed that all classes, either in the main application or loaded classes, ran in the same space.


That's odd. You're now writing the class files into the CLASSPATH space of the system classloader, right? So there's no classloader created or used by your code at all? If that's the case there should be no separation between the classes.
 
James Daniel
Ranch Hand
Posts: 80
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is weird, the class loads and I can invoke it but have no access to any other class or jar. I even loaded the dependant jar via a classloader thinking that they would reside within the same place thus access would be available. Unfortunately, I get a class not found error whenever I try to call that or any other jar. Perplexed..
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!