[英]Java database driver not found, even though it's in the jar file
I have a Java application that connects to a SQLServer database, and it is intended to run at the commandline. 我有一个连接到SQLServer数据库的Java应用程序,它打算在命令行中运行。 In the maven dependencies I have added the following: 在Maven依赖项中,我添加了以下内容:
<dependency>
<groupId>net.sourceforge.jtds</groupId>
<artifactId>jtds</artifactId>
<version>1.3.1</version>
</dependency>
When I run the program via Intellij, I am able to run it with no issues. 通过Intellij运行程序时,我可以毫无问题地运行它。 When I take the jar file that is created and crack it open, I can see the JTDS driver: 当我取出创建的jar文件并将其打开时,可以看到JTDS驱动程序:
However, when I try to run the jar at the commandline with the exact same arguments that I used in the Intellij run configuration, I get the error: 但是,当我尝试使用与Intellij运行配置中使用的参数完全相同的参数在命令行运行jar时,出现错误:
Caused by: java.sql.SQLException: No suitable driver found for jdbc:jtds:sqlserver://databaseserver:1433/database
The command I run is 我运行的命令是
java -jar jarname.jar -cfg ../config.xml
This doesn't seem to be related to this particular driver, as I have the same issue if I connect to an Oracle database and include the Oracle thin driver dependency instead. 这似乎与该特定驱动程序无关,因为如果我连接到Oracle数据库并改为包含Oracle瘦驱动程序依赖项,则会遇到相同的问题。 (However, just to make things fun, if I add a Postgres driver dependency, it runs just fine on the command line connecting to a Postgres database.) (但是,只是为了使事情变得有趣,如果我添加了Postgres驱动程序依赖项,它在连接到Postgres数据库的命令行上运行得很好。)
If I manually add the driver jar (same version as the maven dependency) to the classpath, the jar does run properly. 如果我手动将驱动程序jar(与maven依赖版本相同)添加到类路径,则jar会正确运行。
What could be causing the driver inside the jar to not be on the classpath? 是什么导致jar内的驱动程序不在类路径上? (Or is there something else that could be causing the issue?) (或者是否有其他原因可能导致此问题?)
MadProgrammer在上面的评论是一个问题:我没有注册驱动程序,因为我没有意识到它是JDBC 3.0驱动程序,而不是JDBC 4.0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.