Redian新闻
>
Help!: tomcat classloading problem
avatar
Help!: tomcat classloading problem# Java - 爪哇娇娃
s*n
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!
avatar
s*n
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 的大作中提到】
: 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();
: ...

avatar
c*g
3
I don't think Class in shared can access class in web-inf.

【在 s*********n 的大作中提到】
: 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();
: ...

avatar
s*n
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 的大作中提到】
: I don't think Class in shared can access class in web-inf.
avatar
c*g
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 的大作中提到】
: 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?
:

avatar
m*t
6

With all the repect, that last piece of advice was just bad, bad, bad. 8-)

【在 c**g 的大作中提到】
: 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.

avatar
xt
7

I agree. Making a local resource global is one of the worst things you
can do with classpaths.

【在 m******t 的大作中提到】
:
: With all the repect, that last piece of advice was just bad, bad, bad. 8-)

avatar
c*g
8
hey guys, I was just trying to be nice.

【在 xt 的大作中提到】
:
: I agree. Making a local resource global is one of the worst things you
: can do with classpaths.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。