s*********n 发帖数: 38 | 1 I have a class A, which calls class B.
If I put A in web-inf/, and B in shared/, it will work well.
But now I put A in shared/, and B in web-inf, it doesn't work and give me an
"ClassNotFoundException".
The way A call B is like this:
Class A{
...
Class clas = Class.forName(B);
record = (Record)clas.newInstance();
...
}
How can I solve this problem? I do want to keep the class A in shared/.
Thanks! | s*********n 发帖数: 38 | 2 I read the java doc, and found that there is another method:
public static Class forName(String name,
boolean initialize,
ClassLoader loader)
throws ClassNotFoundException
I guess the problem is that in Tomcat, the web-inf\ and shared\ are using
different classloaders. What I have done above, by default, uses the
classloader of "shared", so that it could not find the class in "web-inf".
I am looking for the way to obtai
【在 s*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I have a class A, which calls class B. : If I put A in web-inf/, and B in shared/, it will work well. : But now I put A in shared/, and B in web-inf, it doesn't work and give me an : "ClassNotFoundException". : The way A call B is like this: : Class A{ : ... : Class clas = Class.forName(B); : record = (Record)clas.newInstance(); : ...
| c**g 发帖数: 274 | 3 I don't think Class in shared can access class in web-inf.
【在 s*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I have a class A, which calls class B. : If I put A in web-inf/, and B in shared/, it will work well. : But now I put A in shared/, and B in web-inf, it doesn't work and give me an : "ClassNotFoundException". : The way A call B is like this: : Class A{ : ... : Class clas = Class.forName(B); : record = (Record)clas.newInstance(); : ...
| s*********n 发帖数: 38 | 4 I have found the solution.
FYI:
use Thread.currentThread().getContextClassLoader() to obtain the classloader
of "web app". Then use this classloader to invoke Class.forName(B, true,
contextClassloader).
done.
but I am still thinking about my another proposal: instruct the "system"
classloader to load classes on the CLASSPATH.
any idea?
an
【在 c**g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I don't think Class in shared can access class in web-inf.
| c**g 发帖数: 274 | 5 Of course you can progmaticly load any class which is accessable
via the file system/network. Why don't you just pack class in the web-inf
and put the jar to shared. Or you can add that web-inf to the global
classpath of tomcat, somewhere in conf/web.xml.
【在 s*********n 的大作中提到】![](/moin_static193/solenoid/img/up.png) : I have found the solution. : FYI: : use Thread.currentThread().getContextClassLoader() to obtain the classloader : of "web app". Then use this classloader to invoke Class.forName(B, true, : contextClassloader). : done. : but I am still thinking about my another proposal: instruct the "system" : classloader to load classes on the CLASSPATH. : any idea? :
| m******t 发帖数: 2416 | 6
With all the repect, that last piece of advice was just bad, bad, bad. 8-)
【在 c**g 的大作中提到】![](/moin_static193/solenoid/img/up.png) : Of course you can progmaticly load any class which is accessable : via the file system/network. Why don't you just pack class in the web-inf : and put the jar to shared. Or you can add that web-inf to the global : classpath of tomcat, somewhere in conf/web.xml.
| xt 发帖数: 17532 | 7
I agree. Making a local resource global is one of the worst things you
can do with classpaths.
【在 m******t 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : With all the repect, that last piece of advice was just bad, bad, bad. 8-)
| c**g 发帖数: 274 | 8 hey guys, I was just trying to be nice.
【在 xt 的大作中提到】![](/moin_static193/solenoid/img/up.png) : : I agree. Making a local resource global is one of the worst things you : can do with classpaths.
|
|