問題描述
當我為 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
環(huán)境變量僅被 java.exe
使用命令,甚至只有在沒有任何-cp
、-classpath
、-jar
參數(shù)時使用.它被 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.
該環(huán)境變量在現(xiàn)實世界中也被認為是一種糟糕的做法,因為它破壞了可移植性.對于 Sun 來說,防止初學者厭倦在 -cp
或 -classpath
參數(shù)中一遍又一遍地輸入相同的類路徑只是有用"的.在現(xiàn)實世界中,首選批處理/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,則類路徑稱為構(gòu)建路徑"(它代表編譯時和運行時類路徑).您可以在項目的屬性中對其進行配置.您可以添加一個完整的文件夾,您可以添加單個/外部 JAR 文件,您可以鏈接項目等.好好利用它.忘記整個 CLASSPATH
環(huán)境變量.
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
這篇關(guān)于無法加載 com.mysql.jdbc.Driver的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!