简体   繁体   English

无法加载com.mysql.jdbc.Driver

[英]Unable to load com.mysql.jdbc.Driver

when I run ant for a Testng application, I am unable to load com.mysql.jdbc.Driver. 当我为Testng应用程序运行ant时,无法加载com.mysql.jdbc.Driver。

Below is the exception thrown. 以下是引发的异常。

[testng] java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
[testng] at java.net.URLClassLoader$1.run(URLClassLoader.java:366)
[testng] at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
[testng] at java.security.AccessController.doPrivileged(Native Method)

Thanks and regards, Sreekanth 谢谢和问候,Sreekanth

The CLASSPATH environment variable is only used by the java.exe command and even then only when used without any of the -cp , -classpath , -jar arguments. CLASSPATH环境变量java.exe命令使用,甚至仅在不带 -cp-classpath-jar参数的情况下使用。 It is ignored by IDE's. 它被IDE 忽略

That environment variable is in real world also considered a poor practice since it breaks portability. 在现实世界中,该环境变量也被认为是不好的做法,因为它破坏了可移植性。 It's only "useful" for Sun to prevent that starters get tired of typing the same classpath again and again in the -cp or -classpath arguments. 对于Sun来说,这只是“有用”,可以防止启动程序厌倦在-cp-classpath参数中一次又一次地键入相同的类路径。 In real world, batch/shell files are preferred. 在现实世界中,批处理/ shell文件是首选。

If you're using an IDE, The classpath is called the "build path" (it represents both compiletime and runtime classpath). 如果您使用的是IDE,类路径被称为“构建路径”(这代表编译时和运行时类路径)。 You can configure it in the project's properties. 您可以在项目的属性中对其进行配置。 You can add a complete folder, you can add individual/external JAR files, you can link projects, etcetera. 您可以添加完整的文件夹,可以添加单个/外部JAR文件,可以链接项目等。 Make use of it. 利用它。 Forget the whole CLASSPATH environment variable. 忘记整个CLASSPATH环境变量。

For command prompt, 对于命令提示符,

You have to put the full path to the jarfile in the classpath (including the filename): 您必须将jar文件的完整路径放在类路径中(包括文件名):

.;C:\j2sdk1.4.2_16\jre\lib;
C:\Program Files\mysql-connector-java-3.1.144\mysql-connector-java-3.1.14-bin.jar

As Hippo said, you have to restart cmd after changing that. 正如河马所说,您必须在更改后重新启动cmd If it doesn't work, launch your program like this: 如果不起作用,请按以下方式启动程序:

java -cp ".;C:\j2sdk1.4.2_16\jre\lib;
C:\Program Files\mysql-connector-java-3.1.144\mysql-connector-java-3.1.14-bin.jar"
my.class.Name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM