【发布时间】:2014-01-08 21:43:29
【问题描述】:
我编写了一个简单的多人 JAVA 应用程序,它可以使用 JDBC 驱动程序连接到 MySQL。 我最终将它打包在 .jar 文件中。当我在本地机器 MacOS 上进行测试时,一切正常。 (我不完全记得设置 JDBC 驱动程序的所有步骤,因为那是一年前我写这个应用程序的时候)。此时应用程序连接到另一台服务器上的外部数据库(我没有更改它,因为它没有问题)。如果是远程数据库的问题,那将是另一个错误。
现在,当我需要在 Windows Server 2003 上投入生产时,我不断收到标准错误:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
And I don't get it, because I think I've done the steps right:
0. Installed Java
1. downloaded this: http://dev.mysql.com/downloads/connector/j/
2. added it to the same folder where the app.jar is
3. wrote in cmd: set CLASSPATH =.;C:\Documents and Settings\Administrator\Desktop\tvplan_exchange\mysql-connector-java-5.1.28-bin.jar
4. tested the variable: echo %CLASSPATH% and it showed the same path
5. runned the app: java -jar app.jar
6. got the error:
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 svagers.Db.connect(Db.java:21)
at svagers.Server.main(Server.java:58)
MYSQL ERRORcom.mysql.jdbc.Driver
好吧,对于那些认为使用这样的应用程序是一个安全错误的人 - 不用担心,这不是问题,我只需要它运行而不考虑风险:)
这是环境: 操作系统:Windows Server 2003(标准版,Service Pack 2) Java:1.7.0_45(SE 运行时环境
【问题讨论】:
-
您是否通过调用:
Class.forName("com.mysql.jdbc.Driver");加载了您的 MySQL 驱动程序? -
这是行:Class.forName(driver).newInstance();其中驱动程序是 com.mysql.jdbc.Driver
-
正如我所说 - 代码中的一切都应该没问题,因为我在我的计算机上启动并运行它(在开发期间我使用我的计算机作为服务器从其他计算机连接)
-
那么你没有正确地将你的mysql-connector依赖添加到类路径中。
-
你能告诉我如何正确地做到这一点吗?我像写的那样添加了它: set CLASSPATH =.;C:\Documents and Settings\Administrator\Desktop\tvplan_exchange\mysql-connector-java-5.1.28-bin.jar 并且该目录存在,并且具有该名称的文件存在也。有什么问题?
标签: java mysql jdbc windows-server-2003