问题
Brief: Tomcat6 can't recognize my ojdbc14.jar in WebRoot/WEB-INF/lib. I suppose I don't need to configure anything, just putting the .jar file in there and it should work.
Details:
I've created a web application and put ojdbc14.jar in WEB-INF/lib folder. When I start Tomcat6 and access an index.jsp page which creates an object that initiates a connection to my oracle database, an error occurs saying "Cannot load JDBC driver class 'oracle.jdbc.OracleDriver'". Apparently Tomcat6 can't recognize the 'oracle.jdbc.OracleDriver' class in web-inf/lib/ojdbc14.jar.
I've done some research and speculate the following reasons:
- Tomcat6 has got some different ways of loading classes. They change it in every version released. Or
- There is a bug with Tomcat6. I found a similar bug in Tomcat3 (see here https://bugs.eclipse.org/bugs/show_bug.cgi?id=249044). But I doubt this bug could last for so many versions.
- Tomcat6 has included ojdbc14.jar's content in its own lib file already, and there's a conflict by adding it to web-inf/lib once more. I don't believe this, because after deleting ojdbc14.jar from WEB-INF/lib, the problem persists.
Here's my web application structure:
- WebRoot
- WEB-INF
- classes
- 'A class for initiating a connection'
- lib
- ojdbc14.jar
- classes
- index.jsp
- WEB-INF
The error:
org.apache.tomcat.dbcp.dbcp.SQLNestedException: Cannot load JDBC driver class 'oracle.jdbc.OracleDriver'
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
at com.demo6.DataSourceExample.getConnection(DataSourceExample.java:18)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:63)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:384)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:228)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:216)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:634)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:445)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.lang.ClassNotFoundException: oracle.jdbc.OracleDriver
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at org.apache.tomcat.dbcp.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:760)
... 21 more
回答1:
The stacktrace tells that you've created a DataSource
as <Resource>
in Tomcat's side. In that case, the JDBC driver JAR file really needs to be put in Tomcat's side as well as it's Tomcat who manages the DataSource
, not your webapp. Drop it in Tomcat/lib
and this exception should disappear. You could also drop it in an external folder and add its path to Tomcat's common classpath as definied by common.loader
property in /conf/catalina.properties
file.
Please note that you don't need to load the driver manually. Just grab the DataSource
and then get the Connection
from it.
来源:https://stackoverflow.com/questions/7222733/tomcat6-ignores-web-inf-lib