Tomcat6 忽略 web-inf/lib
简介: Tomcat6 无法识别 WebRoot/WEB-INF/lib 中的 ojdbc14.jar。我想我不需要配置任何东西,只需将 .jar 文件放在那里就可以了。
详细信息:
我创建了一个 Web 应用程序并将 ojdbc14.jar 放在 WEB-INF/lib 文件夹中。当我启动 Tomcat6 并访问创建一个启动与 Oracle 数据库的连接的对象的 index.jsp 页面时,会出现错误,提示“无法加载 JDBC 驱动程序类 'oracle.jdbc.OracleDriver'”。显然,Tomcat6 无法识别 web-inf/lib/ojdbc14.jar 中的“oracle.jdbc.OracleDriver”类。
我做了一些研究并推测了以下原因:
- Tomcat6 有一些不同的加载类的方式。他们在每个发布的版本中都会更改它。或者
- Tomcat6 有错误。我在 Tomcat3 中发现了类似的错误(请参见此处 https://bugs.eclipse.org /bugs/show_bug.cgi?id=249044)。但我怀疑这个错误是否会持续这么多版本。
- Tomcat6已经将ojdbc14.jar的内容包含在自己的lib文件中,再次将其添加到web-inf/lib中会发生冲突。我不相信这一点,因为从WEB-INF/lib中删除ojdbc14.jar后,问题仍然存在。
这是我的网络应用程序结构:
- WebRoot
- WEB-INF
- 课程
- “用于启动连接的类”
- 库
- ojdbc14.jar
- 课程
- index.jsp
- WEB-INF
错误:
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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
堆栈跟踪表明您已在 Tomcat 端创建了一个
DataSource
作为
。在这种情况下,JDBC 驱动程序 JAR 文件确实需要放在 Tomcat 一侧,并且由 Tomcat 管理DataSource
,而不是您的 Web 应用程序。将其放入Tomcat/lib
中,此异常就会消失。您还可以将其放入外部文件夹中,并将其路径添加到由/conf/catalina.properties
文件中的common.loader
属性定义的 Tomcat 公共类路径中。请注意,您不需要手动加载驱动程序。只需获取
DataSource
,然后从中获取Connection
。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 theDataSource
, not your webapp. Drop it inTomcat/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 bycommon.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 theConnection
from it.