-
1. Re: Classloader rules... what are they?
tbfmicke Aug 29, 2002 3:32 AM (in response to javajohnson)I read somewhere that you should use the context classloader from the thread you are in.
So you could try the following:
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class theClass = loader.findClass(TheClassName);
Disclaimer: I'm not sure if this will work, and if it works I'm not sure about the exact reason why it works :-)
Regards -
2. Re: Classloader rules... what are they?
javajohnson Aug 29, 2002 11:37 AM (in response to javajohnson)Well, thanks for the suggestion, but it didn't work. I still got the ClassNotFoundException
Incidentially, i had to change:
Class theClass = loader.findClass(TheClassName);
to
Class theClass = Class.forName(TheClassName,true,loader);
because findClass is protected.
Back to the drawing board, anyone else have any suggestions? -
3. Re: Classloader rules... what are they?
mipe Aug 29, 2002 12:05 PM (in response to javajohnson)hi,
I was also wrestling with classloaders, and I had to dig deep into the secrets of classloaders, and now I can use them good enough.
One thing I just guess:
(1) The class you want to load is located in your EJB jar or ear or (2) you want to load from a different location (eg. an external jar)
If it is the 1. case, I don't really know why it throws ClassNotFoundException. In fact, I tried it, and it worked.
If it is the 2. case, you should use something like this:
ClassLoader oldCL = Thread.currentThread().getContextClassLoader();
try {
URLClassLoader classloader = URLClassLoader.newInstance(
new URL[]{new URL(classPath)}, Thread.currentThread().getContextClassLoader());
Thread.currentThread().setContextClassLoader(classloader);
Class c = classloader.loadClass(className);
return c.newInstance();
} finally {
Thread.currentThread().setContextClassLoader(oldCL);
}
sorry for the ugly formatting, but whitespaces are removed.
So, I use this code loading a class from a jar file that is located on a http server. -
4. Re: Classloader rules... what are they?
mipe Aug 29, 2002 12:14 PM (in response to javajohnson)by the way,
you should check this. very good stuff how jboss classloaders works:
http://www.jboss.org/modules/bb/index.html?module=bb&op=viewtopic&t=forums/ -
5. Re: Classloader rules... what are they?
javajohnson Aug 30, 2002 2:44 PM (in response to javajohnson)Thanks for all the advice. I ended up not being able to get the Class.forName call to work while within the bean method, so I just moved the functionality to a class earlier up the chain and passed the Class object to the bean. Oh well. It isn't the cleanest, but I have to move on!
-
6. Re: Classloader rules... what are they?
kashpaw Nov 23, 2002 1:17 AM (in response to javajohnson)I assume the class resides in some non-standard location (i.e. not in an archive in the lib or deploy directories). If that's the case, you can specify the location in the configuration of the first mbean in jboss-service.jar.
-
7. Re: Classloader rules... what are they?
bpowell Nov 23, 2002 6:41 AM (in response to javajohnson)Mipe,
regarding slsb and classloading,
2 questions:
If you use classforname or get a new instance of any class in this stateless bean, can the container manage transactions in this class?
If we use the thread's classloader can we guarantee we are thread safe in the new class?