Add JAR to WAR's classpath without changing the WAR file

痞子三分冷 提交于 2019-12-25 18:41:24

问题


I deploy a WAR file in Tomcat. In addition I have a JAR file.

Is it possible to make the classes in the JAR file available to services in the WAR file without changing the WAR file, or does it have be added to the WAR's WEB-INF/lib?

I've tried adding the JAR to lib/, but it doesn't work. Spring fails with org.springframework.beans.factory.BeanCreationException » java.lang.NoClassDefFoundError upon startup.

EDIT

I realized that I might have made myself an unsolvable and architectually ugly problem. Here's the thing:

  • The WAR has a reference <bean id="warService" class="com.war.Service" />
  • My JAR has a class com.mystuff.MyService that extends com.war.Service
  • I have replaced the bean reference with <bean id="warService" class="com.mystuff.MyService" />
  • Spring fails with java.lang.NoClassDefFoundError: com.war.Service

回答1:


Or you could put it(your jar file) in your Tomcat root directory under lib folder. Chk this out :

http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html

Per your edit

  1. Something is wrong with your jar
  2. You have mistaken the fully qualified class name in your application context

I've happen to have mysql-connector-java-5.0.8-bin jar in my lib folder. And just for test I've put this in my application context :

<bean id="driver" class="com.mysql.jdbc.Driver" />

Worked as charm, check those two cases above




回答2:


You can use tomcat's lib directory but all jar added there has been shared for all app in your server.




回答3:


While not recommended, you could use the Tomcat "Common" classloader for this scenario.

http://tomcat.apache.org/tomcat-6.0-doc/class-loader-howto.html



来源:https://stackoverflow.com/questions/10481238/add-jar-to-wars-classpath-without-changing-the-war-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!