且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

gradle jettyrun oracle找不到合适的驱动程序

更新时间:2023-11-16 18:06:58

使用JDBC .jar文件不小心在类路径中加载.jar的位置,您可能会看到类似于您所看到的错误。如果您想确定,请将类路径上的.jar作为参数传递给调用该程序的主JVM。原因是您需要确保.jar类文件在默认JVM类加载器中加载。如果试图通过其他方式动态加载JDBC .jars(或许甚至是OSGI),或者像Tomcat那样通过自定义类加载器动态加载,那么您可能会遇到类加载器问题。


This is a kind of driving me nuts, I found some similar questions here on SO but I can't get it to work.

I have a multi project spring (web) project. It starts fine, but as soon as I want to query the db I get a

Nested in org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is org.apache.commons.dbcp.SQLNestedException: 
Cannot create JDBC driver of class 'oracle.jdbc.OracleDriver' for connect URL 'jdbc:oracle:thin//server:1521:XE':

I tried many things: I made sure the ojdbc.jar is inside my project (not part of maven repo so I loaded it with gradle like

compile files('lib/websphere_apis.jar','lib/ojdbc14.jar') 

in my sub (not web) or in my web project.

Which gets the jars inside war when I generate them with with gradle war

I tried adding the jars to the jettyRun configuration with

jettyRun {
   additionalRuntimeJars = files('lib/ojdbc14.jar')
}

I'm a bit in doubt whether the files stmt is correct but still it doesn't work.

I also found on SO that I should put

   providedRuntime files("$projectDir/../lib/ojdbc14.jar")

inside my webapp as a standard dependency but that still doesn't work for me. I have used postgres driver jars before which I could just include as a dependency.

With JDBC .jar files, if you are not careful about where you load the .jar on the classpath, you might get an error like what you are seeing. If you want to be sure, pass the .jar on the classpath as an argument to the main JVM that is calling the program. The reason is that you need to make sure the .jar class files are loaded in the "default JVM classloader". If you try to load JDBC .jars dynamically through other means ( perhaps even as OSGI) , or dynamically loaded by custom classloader like Tomcat has, then you could get class loader issues.