This week's book giveaway is in the Testing forum. We're giving away four copies of Data Structures the Fun Way: An Amusing Adventure with Coffee-Filled Examples and have Jeremy Kubica on-line! See this thread for details.
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..
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.
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.
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.
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.
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 ]
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.
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..
Roses are red, violets are blue. Some poems rhyme and some don't. And some poems are a tiny ad.