[英]java commandline: java.lang.ClassNotFoundException when JAR is in current folder but not when it is decompressed
A simple Java code has this line:一个简单的 Java 代码有这一行:
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
The code fail at this line with exception代码在这一行失败,但有异常
Exception in thread "main" java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver
When I put sqljdbc41.jar in current folder.当我将 sqljdbc41.jar 放入当前文件夹时。 This jar has above driver.
jar 有以上驱动。 I also have included current folder "."
我还包括了当前文件夹“。” in CLASSPATH env variable.
在 CLASSPATH 环境变量中。
However above code line does not give any problem and code runs successfully when I unzip this jar file, which creates folder hierarchy "com\microsoft\sqlserver\jdbc" in the current folder.但是,当我解压缩此 jar 文件时,上面的代码行没有任何问题,代码运行成功,该文件在当前文件夹中创建文件夹层次结构“com\microsoft\sqlserver\jdbc”。
Where I am missing?我在哪里失踪? How can I set CLASSPATH?
如何设置 CLASSPATH? How can I include jar files when running code using java command line?
使用 java 命令行运行代码时,如何包含 jar 文件?
Instead of using the 4-year-old JDBC driver sqljdbc41.jar directly, you should use a dependency management tool, for example, Maven .与其直接使用4年前的JDBC驱动sqljdbc41.jar ,不如使用依赖管理工具,例如Z9115C10980645C41317ZF244AC1A377F Then, as described in the driver home page , it can be added to a Maven project by adding it as a dependency in the POM file with the following code:
然后,如驱动程序主页中所述,可以通过将其作为依赖项添加到 POM 文件中,使用以下代码将其添加到 Maven 项目中:
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>10.2.1.jre17</version>
</dependency>
See the Maven Central Repository for the latest version.有关最新版本,请参阅Maven 中央存储库。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.