com.mysql.jdbc.Driver 的类未找到异常不是类路径问题

发布于 2024-10-05 10:28:50 字数 1157 浏览 2 评论 0原文

我已经为此工作了几个星期,但现在我只是在原地踏步。我收到运行时错误:“在获取连接 com.mysql.jdbc.Driver 时,在 [class] 中未找到类异常”。

连接函数如下:

/**
 * @return a MySQL connection to the server as specified above 
 */
private static Connection getConnection() {
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(url, username, password);
  } catch (ClassNotFoundException e) {
    System.err.println("class not found exception in [class] while getting connection " + e.getMessage());
  } catch (SQLException e) {
    System.err.println("sql exception in [class] while getting connection " + e.getMessage());
  }
  return con;
} //end getConnection

我已经设置了$CLASSPATH一百次一百种方法,但仍然没有运气。现在看起来是这样的:

# echo $CLASSPATH
:/usr/share/java/mysql-connector-java-5.1.13-bin.jar:/usr/share/java/mysql-connector-java-3.0.17-ga-bin.jar:

这两个文件都存在,并且 Driver.class 文件位于正确的位置。

我什至尝试在导出 jar 之前将类导入到我的代码中,但这导致了一系列其他问题。

我正在使用 Eclipse 3.5.2,服务器正在运行 Fedora 13 x86_64

请帮忙。

编辑:我使用以下命令在服务器上远程运行代码:

# java -jar program.jar

I've been working on this for weeks, and now I'm just running in circles. I'm getting the runtime error: " class not found exception in [class] while getting connection com.mysql.jdbc.Driver ".

The connection function is as follows:

/**
 * @return a MySQL connection to the server as specified above 
 */
private static Connection getConnection() {
  Connection con = null;
  try {
    Class.forName("com.mysql.jdbc.Driver");
    con = DriverManager.getConnection(url, username, password);
  } catch (ClassNotFoundException e) {
    System.err.println("class not found exception in [class] while getting connection " + e.getMessage());
  } catch (SQLException e) {
    System.err.println("sql exception in [class] while getting connection " + e.getMessage());
  }
  return con;
} //end getConnection

I have set the $CLASSPATH a hundred times a hundred ways, and still no luck. This is what it looks like now:

# echo $CLASSPATH
:/usr/share/java/mysql-connector-java-5.1.13-bin.jar:/usr/share/java/mysql-connector-java-3.0.17-ga-bin.jar:

Both of those files exist and have the Driver.class file in them in the right place.

I even tried importing the class into my code before exporting the jar, but that caused a world of other problems.

I'm using Eclipse 3.5.2 and the server is running Fedora 13 x86_64

Please help.

edit: I'm running the code remotely on my server with the command:

# java -jar program.jar

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

作妖 2024-10-12 10:28:50

假设您在 Eclipse 下运行它,请尝试在 Eclipse 运行设置对话框中添加对 jar 的引用。
菜单运行->运行配置->你的项目->类路径选项卡 ->用户条目 ->添加罐子...

Assuming you run it under Eclipse, try to add a reference to the jars in the Eclipse Run setting dialog.
Menu Run -> Run Configuration -> you project -> Classpath tab -> User Entries -> Add Jar...

冷弦 2024-10-12 10:28:50

如果您要在 Apache Tomcat 上部署,请将 JDBC 连接器 jar 文件 (mysql-connector-java-5.0.8-bin.jar) 放在 Tomcat 主目录的 lib 文件夹 <代码>\apache-tomcat-6.0.29\lib\。

有些线程说它是 \common\lib\,但在我的例子中,6.0 上没有找到 common 目录。

并且不要忘记重新启动服务器。

If you are deploying on Apache Tomcat, place your JDBC connector jar file (mysql-connector-java-5.0.8-bin.jar) on lib folder of Tomcat home \apache-tomcat-6.0.29\lib\.

Some threads say its <tomcat_home>\common\lib\, but there is no common directory found on 6.0 in my case.

And don't forget to restart the server.

风流物 2024-10-12 10:28:50

在Eclipse中有一个简单的方法可以解决这个问题:

  1. 右键单击您的项目
  2. 选择“属性”
  3. 选择“部署程序集”
  4. 单击添加->; “Java 构建路径条目”
  5. 选择包含数据库连接器的用户库(例如 mysql-connector-java-5.1.20.jar)。

There is an easy way to solve this problem in Eclipse:

  1. Right Click on your project
  2. Select "Properties"
  3. Select "Deployment Assembly"
  4. Click Add -> "Java Build Path Entries"
  5. Select your user library that contains your database connector (for example, mysql-connector-java-5.1.20.jar).
独木成林 2024-10-12 10:28:50

问题已修复:我必须编辑清单文件以将 ClassPath: 变量包含到 Eclipse 之外所需的文件中,因为 Eclipse 不允许使用简单的方法来完成此操作。使用 JAR 文件时,服务器的类路径并不重要,重要的是 jar 的类路径。

Problem has been fixed: I had to edit the manifest file to include the ClassPath: variable to the files I needed outside of eclipse, because eclipse does not allow an easy way to do it. When using JAR files, the classpath of the server doesn't matter, only that of the jar.

野侃 2024-10-12 10:28:50

我发现我必须转到

“包属性”(右键单击项目/包名称并在子菜单中找到“属性”,它可能位于底部)。

Java 构建路径
单击“订购和导出”选项卡

,然后勾选我想要包含在 jar 导出中的库旁边的空复选框。
(在我的例子中是 ojdbc6.jar)

I found that I had to goto

Package Properties (Right click on project/package name and find Properties in the sub menu it might be at the bottom).

Java Build Path
Click the tab 'Order and Export'

Then tick the empty check box next to the library I wanted to include in the jar export.
(In my case it was ojdbc6.jar)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文