com.mysql.jdbc.Driver 的 ClassNotFoundException - 未使用 IDE [英] ClassNotFoundException for com.mysql.jdbc.Driver -not using an IDE
问题描述
我正在尝试将 MySQL 驱动程序用于 Java,但我不断收到 ClassNotFoundException.我根本没有使用 IDE,该程序是用 Vim 编写的,并在 Window 的命令行中编译.这是一个班级,教授告诉我们我们根本不能使用 IDE.我将 JAR 文件添加到 Window 的 PATH 系统环境变量中,但我仍然收到错误消息.我一定是做错了什么,但我所做的大部分研究都表明您需要将驱动程序添加到 IDE 的类路径中,但我没有使用 IDE,只是使用 Windows.
I am trying to use the MySQL driver for java and I keep getting a ClassNotFoundException. I am not using an IDE at all, the program is being written in Vim and compiled at the Window's command line. It's for a class and the professor has told us we cannot use an IDE at all. I added the JAR file to the Window's PATH System Environment Variable but I'm still getting the error. I must be doing something wrong but most of the research I've done just pulls up that you need to add the driver to your IDE's classpath but I'm not using an IDE, just Windows.
这是我的代码:
try {
Class.forName("com.mysql.jdbc.Driver");
}
catch (ClassNotFoundException e) {
System.out.println("Where is your MySQL JDBC Driver?");
e.printStackTrace();
return;
}
和错误:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at sqlAccess.<init>(sqlAccess.java:21)
at Tester.DoOther(Tester.java:44)
等
这是我添加到系统变量下的 PATH
变量中的内容:
And this is what I added to my PATH
variable under System Variables:
C:\Program Files\Java\jdk1.7.0_11\bin\mysql-connector-java-5.1.24-bin.jar
我错过了什么?
推荐答案
您必须将 jar 的路径添加到 CLASSPATH,而不是 PATH
You must add path to jar to CLASSPATH, not PATH
set "CLASSPATH=.;F:\Software Tech II\FinalProject\lib\mysql-connector-java-5.1.24-bin.jar"
java -cp %CLASSPATH% Tester
这篇关于com.mysql.jdbc.Driver 的 ClassNotFoundException - 未使用 IDE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!