问题描述
当我为 Testng 应用程序运行 ant 时,我无法加载 com.mysql.jdbc.Driver.
when I run ant for a Testng application, I am unable to load com.mysql.jdbc.Driver.
下面是抛出的异常.
[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)
谢谢和问候,斯里坎特
推荐答案
CLASSPATH
环境变量仅被 java.exe
使用命令,甚至只有在没有任何-cp
、-classpath
、-jar
参数时使用.它被 IDE 忽略.
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. It is ignored by IDE's.
该环境变量在现实世界中也被认为是一种糟糕的做法,因为它破坏了可移植性.对于 Sun 来说,防止初学者厌倦在 -cp
或 -classpath
参数中一遍又一遍地输入相同的类路径只是有用"的.在现实世界中,首选批处理/shell 文件.
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. In real world, batch/shell files are preferred.
如果您使用的是 IDE,则类路径称为构建路径"(它代表编译时和运行时类路径).您可以在项目的属性中对其进行配置.您可以添加一个完整的文件夹,您可以添加单个/外部 JAR 文件,您可以链接项目等.好好利用它.忘记整个 CLASSPATH
环境变量.
If you're using an IDE, The classpath is called the "build path" (it represents both compiletime and runtime classpath). 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. Make use of it. Forget the whole CLASSPATH
environment variable.
对于命令提示符,
您必须将 jarfile 的完整路径放在类路径中(包括文件名):
You have to put the full path to the jarfile in the classpath (including the filename):
.;C:\j2sdk1.4.2_16\jre\lib;
C:\Program Files\mysql-connector-java-3.1.144\mysql-connector-java-3.1.14-bin.jar
正如河马所说,更改后必须重新启动cmd
.如果它不起作用,请像这样启动您的程序:
As Hippo said, you have to restart cmd
after changing that. 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
这篇关于无法加载 com.mysql.jdbc.Driver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!