问题
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
- Something is wrong with your jar
- 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