简体   繁体   English

找不到Java数据库驱动程序,即使它位于jar文件中

[英]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驱动程序:

jar文件展开

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.

相关问题 即使用户库containg jar文件位于java构建路径中,eclipse中也没有找到类异常 - class not found exception in eclipse even though the user library containg jar file is in java build path JDBC 找不到合适的驱动程序,即使 .jar 文件已添加到类路径和依赖项 - JDBC No Suitable Driver found Even Though .jar Files are added to the Class Path and Dependencies jGrasp即使在类路径中也找不到jar文件 - jGrasp can't find jar file even though it's in the classpath 尽管我有 jar 文件,但在 java 中找不到类异常 - Class not found exception in java though I have the jar file 找不到SQL驱动程序 - 但实现了驱动程序JAR文件 - Not SQL Driver Found - but Driver JAR File is implemented 即使包含.jar文件,也不会出现NoClassDefFoundError - NoClassDefFoundError even though .jar file is included classpath缺少jar文件,即使指定了 - classpath missing jar file even though specified java.lang.ClassNotFoundException:Eclipse中的com.mysql.jdbc.Driver,即使已引用mysql-connector jar - java.lang.ClassNotFoundException: com.mysql.jdbc.Driver in Eclipse even though the mysql-connector jar is referenced class not found exception com.mysql.jdbc.driver甚至我添加了jar文件 - class not found exception com.mysql.jdbc.driver even I added the jar file 即使有 jar 文件,也无法将 Java 连接到 MySQL 数据库 - Cannot connect Java to MySQL database even after having jar file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM